简体   繁体   中英

How to update dropDownList on the onTextChange event of a textBox

I have a textBox called txtDate

   <asp:TextBox ID="txtDate" runat="server" AutoPostBack="true" Width="120px" 
    ontextchanged="txtDate_TextChanged" ></asp:TextBox>

And also I have a dropDownList called DropDownList1

    <asp:DropDownList ID="DropDownList1" runat="server" 
                DataSourceID="SqlDataSource2" DataTextField="sTime" DataValueField="sTime" 
                AutoPostBack="True">
    </asp:DropDownList>

DropDownList is taking data from a sqlDataSourse called SqlDataSource2 . I need to update the dropDownList on the onTextChange event of a textBox . So I wrote this.

    protected void txtDate_TextChanged(object sender, EventArgs e)
    {
       SqlDataSource2.SelectCommand = "NEW SQL COMMAND";
    }

But this does not update the dropDownList . Please help me if anybody can.

I found it. I changed the code of the TextChanged event to this to this.

     protected void txtDate_TextChanged(object sender, EventArgs e)
     {
          SqlDataSource2.SelectCommand = "NEW SQL COMMAND";
          DropDownList1.DataSourceID = "SqlDataSource2";
     }

You need dynamic binding here. Change the dropdown HTML to this

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
</asp:DropDownList> 

protected void txtDate_TextChanged(object sender, EventArgs e)
{

   /* This is not actual code but a kind of algorithm to proceed with*/

    using(SqlConnection con = new SqlConection(you_conn_string))
    {
       using (command)
       {
           usin(SqlDataReader rdr = cmd.ExecuteReader())
           {
               var dt = new Datatable();
               dt.load(rdr);
               dropdownlist1.datasource = dt;
               dropdownlist1.datatextfield="textfield";
               dropdownlist1.datavaluefield="valuefield";
               dropdownlist1.databind();

       }
     }
}
SqlDataSource2.SelectCommand = "NEW SQL COMMAND";
DataView testView = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable table = testView .ToTable();

DropDownList1.Datasource = table;
DropDownList1.Databind();

Declare first four lines globally,so that no need to define again and again.

If you are using this code remove datatext,datavalue,datasourceID from dropdownlist .aspx code..

protected void txtDate_TextChanged(object sender, EventArgs e)
{

  SqlConnection con = new SqlConection(you_conn_string)
  SqlCommand cmd=new SqlCommand();
  SqlDataAdapter da=new SqlDataAdapter();
  Dateset ds=new Dataset();
  cmd = new SqlCommand("procedure name to get data", con);  
  cmd.CommandType = CommandType.StoredProcedure;
  da = new SqlDataAdapter(cmd);
  da.Fill(ds);
  dropdownlist1.datasource = ds;
  dropdownlist1.datatextfield="textfield";
  dropdownlist1.datavaluefield="valuefield";
  dropdownlist1.databind();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM