0

我的asp.net面板有问题我想在更改另一个下拉框的索引时修改下拉框数据源选择命令,但是每次我更改新的下拉框都会创建我的代码:

   <asp:UpdatePanel ID="UpdatePanel1" runat="server"  >
          <ContentTemplate>
              <asp:DropDownList ID="comboCountry" runat="server"   AutoPostBack="True"
                    DataSourceID="SqlDataSource3"  OnSelectedIndexChanged="comboCountry_OnSelectedIndexChanged"
                    DataTextField="country_name" DataValueField="country_id">

                  </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:JobsConnectionString %>" 
                    SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
                </td>
             </tr>
            <tr class="trwidth">
            <td>
                <asp:DropDownList ID="comboCity" runat="server" DataSourceID="SqlDataSource2"  
                    DataTextField="city_name" DataValueField="location_id">
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:JobsConnectionString %>" 
                    SelectCommand="SELECT * FROM [location]"></asp:SqlDataSource> 
        </ContentTemplate>
    </asp:UpdatePanel>

后面的代码:

protected void comboCountry_OnSelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        SqlDataSource2.SelectCommand = "SELECT * FROM [location] where [country_id]=" + comboCountry.SelectedValue;            
       // SqlDataSource2.DataBind();
        comboCity.DataBind();
    }
    catch (Exception exception)
    {
        Debug.WriteLine(exception.Message);
    }
}
4

1 回答 1

0

在您的第一个下拉菜单中选择 country_code 的值,然后将第二个下拉参数绑定到第一个下拉菜单。然后将第一个下拉列表的自动回发设置为 true,第二个下拉列表将呈现结果。

于 2013-01-08T20:48:57.193 回答