Sie Ste

Sie Ste

  • NA
  • 196
  • 183.7k

Dropdown list on web form

Dec 9 2011 1:01 PM
On a webform page 2010 written in C#  I have a dropdown list box where I would like to make the following changes:

1. When the list box is displayed iniitally, I would like the selected value to be a literal 'Select Payment Date'. This would
be a value I load in the LoadPaymentDates method.
(The values from the dropdown list box are loaded with the code from LoadPaymentDates method.)
2. Once the user has selected a value from the dropdown list, I would like the dropdown list to display that selection. Basically i
I would like whatever the selects from the dropdown list to become the selected value. TRight now right after the user selects a
value, the dropdown list defaults to displaying the original values.

Code for aspx file:

<asp:DropDownList ID="ddlPaymentDate" runat="server" AutoPostBack="true" OnSelectedIndexChanged="SaveSelectPaymentDate"> 
  </asp:DropDownList>

I would like

Code for aspx.cs file:

  protected void Page_Load(object sender, EventArgs e)
  {
  LoadPaymentDates();
  }


  protected void LoadPaymentDates()
  {
  String strPaymentDateSql = ""; 
  strPaymentDateSql = "select payment_date from dbo.ASch order by payment_date desc";
  FillAttListBox(ddlPaymentDate, "payment_date", "payment_date", DBConnection.ExecuteQuery(strPaymentDateSql), false); 
  }

protected void SaveSelectPaymentDate(object sender, EventArgs e)
  {
  ddlPaymentDate.SelectedValue = ddlPaymentDate.SelectedValue;
  }


public static void FillAttListBox(DropDownList AttlistBox, String dataValueField, string dataTextField, DataTable dataTbl, bool bHasBlank)
  {
  AttlistBox.DataTextField = dataTextField;
  AttlistBox.DataValueField = dataValueField;
  AttlistBox.DataSource = dataTbl;
  AttlistBox.DataBind();

 
  }

  public static DataTable ExecuteQuery(string SQLstring)
  {
  string constr = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
  SqlConnection conn = new SqlConnection(constr);
  DataTable dt = new DataTable("tbl");

  using (conn)
  {
  conn.Open();
  SqlCommand comm = new SqlCommand(SQLstring, conn);
  comm.CommandTimeout = 0;
  SqlDataAdapter da = new SqlDataAdapter(comm);
  da.Fill(dt);
  }

  return dt;
  }

Answers (1)