2013-09-05 1 views
0

j'ai un textBox appelé txtDateComment mettre à jour DROPDOWNLIST sur l'événement onTextChange d'un textBox

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

Et aussi j'ai un dropDownList appelé DropDownList1

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

DropDownList prend des données d'un sqlDataSourse appelé SqlDataSource2. Je dois mettre à jour le dropDownList sur l'événement onTextChange d'un textBox. Alors j'ai écrit ça.

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

Mais cela ne met pas à jour le dropDownList. S'il vous plaît aidez-moi si quelqu'un peut.

Répondre

0

Je l'ai trouvé. J'ai changé le code de l'événement TextChanged à ceci.

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

Vous avez besoin d'une liaison dynamique ici. Modifier le code HTML déroulant à cette

<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(); 

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

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

Déclare quatre premières lignes globalement, de sorte que pas besoin de définir encore et encore.

Si vous utilisez ce supprimer le code DataText, datavalue, DataSourceID à partir du code dropdownlist .aspx ..

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(); 

} 
Questions connexes