根据用户输入动态创建多个下拉列表

时间:2014-06-05 03:13:48

标签: c# asp.net

我有一种情况,我有1个下拉列表,其中包含数字1 ..... n作为列表项。 我必须以这样的方式编写代码:如果我从列表中选择3,则会创建3个新的下拉列表,每个下拉列表都链接到其项目的唯一sql数据源。

我真的很困惑。

我真的很感激一点指导。

谢谢

1 个答案:

答案 0 :(得分:1)

好的拳头你需要在.aspx网页表格上有下拉列表和占位符控件,如下所示:

 <asp:DropDownList ID="DropDownList1" runat="server" 
                   OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
 </asp:DropDownList>

 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

然后在您的DropDownList1_SelectedIndexChanged方法后面的代码中,您需要检查下拉列表的值并在如下所示的循环中创建下拉列表:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int count = Convert.ToInt32(DropDownList1.SelectedItem.Value);

        for (int i = 0; i < count; i++ )
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = "ddl" + i;
            ddl.DataSource = //your data source here
            ddl.DataBind();
            PlaceHolder1.Controls.Add(ddl);
        }
    }

如果您有更多问题,请与我们联系。