【问题标题】:asp.net update panel create new dropdownboxasp.net 更新面板创建新的下拉框
【发布时间】:2013-01-08 19:03:50
【问题描述】:

我的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);
    }
}

【问题讨论】:

  • 您能详细解释一下问题所在吗?你得到什么结果,你想要什么结果?
  • 当我更改组合框 (comboCountry) 的索引时,它应该调用方法 comboCountry_OnSelectedIndexChanged 并更改 SqlDataSource2 的数据源选择命令,然后应该更新组合框 (comboCity) 项目,而不是更新组合框 (comboCity) 项目获取包含新项目的新组合框。
  • “新项目”是什么意思?
  • 我的意思是这个

标签: asp.net combobox updatepanel dropdownbox


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多