0

我有一个textBoxtxtDate

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

而且我还有一个dropDownListDropDownList1

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

DropDownList 正在从名为 的 sqlDataSourse 中获取数据SqlDataSource2。我需要更新 dropDownList 的onTextChange事件textBox。所以我写了这个。

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

但这不会更新dropDownList. 如果有人可以,请帮助我。

4

4 回答 4

0

我找到了。我将 TextChanged 事件的代码更改为此。

     protected void txtDate_TextChanged(object sender, EventArgs e)
     {
          SqlDataSource2.SelectCommand = "NEW SQL COMMAND";
          DropDownList1.DataSourceID = "SqlDataSource2";
     }
于 2013-09-05T18:05:39.180 回答
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();

       }
     }
}
于 2013-09-05T18:24:40.410 回答
0
SqlDataSource2.SelectCommand = "NEW SQL COMMAND";
DataView testView = (DataView)SqlDataSource2.Select(DataSourceSelectArguments.Empty);
DataTable table = testView .ToTable();

DropDownList1.Datasource = table;
DropDownList1.Databind();
于 2013-09-06T04:48:28.897 回答
0

全局声明前四行,这样就不用再定义了。

如果您使用此代码,请从下拉列表 .aspx 代码中删除 datatext、datavalue、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();

}
于 2013-09-06T04:54:42.860 回答