2013-09-05 41 views
0

我有一個textBox稱爲txtDate如何在一個文本框的onTextChange事件

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

而且也對我有dropDownList稱爲DropDownList1

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

DropDownList會從一個叫sqlDataSourse採集數據更新DROPDOWNLIST SqlDataSource2。 我需要更新textBoxonTextChange事件上的dropDownList。所以我寫了這個。

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

但是這並不更新dropDownList。如果有人願意,請幫助我。

回答

0

我找到了。 我將TextChanged事件的代碼更改爲此。

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

這裏需要動態綁定。下拉HTML更改爲這個

<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

聲明全球前四線,所以沒有必要一次又一次的定義。

如果您使用此代碼刪除datatext,datavalue,從下拉列表的.aspx代碼的DataSourceID ..

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

} 
相關問題