C# 如何在ASP.NET中动态创建下拉控件

C# 如何在ASP.NET中动态创建下拉控件,c#,sql,asp.net,webforms,C#,Sql,Asp.net,Webforms,我已经创建了5个下拉列表。它来自aspx.cs for (int i = 0; i < 5; i++) { DropDownList drop = new DropDownList(); drop.ID = "dropdownlist" + i; form1.Controls.Add(drop); form1.Controls.Add(ne

我已经创建了5个下拉列表。它来自aspx.cs

for (int i = 0; i < 5; i++)
            {
                DropDownList drop = new DropDownList();
                drop.ID = "dropdownlist" + i;
                form1.Controls.Add(drop);
                form1.Controls.Add(new LiteralControl("<br />"));
             }

如何使用此代码动态创建每个新的5个下拉列表?

请查看Mysterio11&Win的评论

下面是填充数据的基本思想。最重要的是,它没有优化。在循环中创建命令和连接是个坏主意

        if (!this.IsPostBack)
        {
            DropDownList DropDownList1;
            for (int i = 0; i < 5; i++)
            {
                DropDownList1 = (DropDownList)FindControl("dropdownlist" + i);
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT ID, Name FROM RejectedProduct"))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        DropDownList1.DataSource = cmd.ExecuteReader();
                        DropDownList1.DataTextField = "Name";
                        DropDownList1.DataValueField = "ID";
                        DropDownList1.DataBind();
                        con.Close();
                    }
                }
                DropDownList1.Items.Insert(0, new ListItem("Select Item for adding", "0"));
            }
        }
if(!this.IsPostBack)
{
DropDownList DropDownList1;
对于(int i=0;i<5;i++)
{
DropDownList1=(DropDownList)FindControl(“DropDownList”+i);
string constr=ConfigurationManager.ConnectionString[“constr”].ConnectionString;
使用(SqlConnection con=newsqlconnection(cont))
{
使用(SqlCommand cmd=newsqlcommand(“从RejectedProduct中选择ID、名称”))
{
cmd.CommandType=CommandType.Text;
cmd.Connection=con;
con.Open();
DropDownList1.DataSource=cmd.ExecuteReader();
DropDownList1.DataTextField=“Name”;
DropDownList1.DataValueField=“ID”;
DropDownList1.DataBind();
con.Close();
}
}
DropDownList1.Items.Insert(0,新列表项(“选择要添加的项”,“0”));
}
}

请查看Mysterio11&Win的评论

下面是填充数据的基本思想。最重要的是,它没有优化。在循环中创建命令和连接是个坏主意

        if (!this.IsPostBack)
        {
            DropDownList DropDownList1;
            for (int i = 0; i < 5; i++)
            {
                DropDownList1 = (DropDownList)FindControl("dropdownlist" + i);
                string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT ID, Name FROM RejectedProduct"))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Connection = con;
                        con.Open();
                        DropDownList1.DataSource = cmd.ExecuteReader();
                        DropDownList1.DataTextField = "Name";
                        DropDownList1.DataValueField = "ID";
                        DropDownList1.DataBind();
                        con.Close();
                    }
                }
                DropDownList1.Items.Insert(0, new ListItem("Select Item for adding", "0"));
            }
        }
if(!this.IsPostBack)
{
DropDownList DropDownList1;
对于(int i=0;i<5;i++)
{
DropDownList1=(DropDownList)FindControl(“DropDownList”+i);
string constr=ConfigurationManager.ConnectionString[“constr”].ConnectionString;
使用(SqlConnection con=newsqlconnection(cont))
{
使用(SqlCommand cmd=newsqlcommand(“从RejectedProduct中选择ID、名称”))
{
cmd.CommandType=CommandType.Text;
cmd.Connection=con;
con.Open();
DropDownList1.DataSource=cmd.ExecuteReader();
DropDownList1.DataTextField=“Name”;
DropDownList1.DataValueField=“ID”;
DropDownList1.DataBind();
con.Close();
}
}
DropDownList1.Items.Insert(0,新列表项(“选择要添加的项”,“0”));
}
}

在asp.net中使用动态控件是个坏主意,因为控件在每次回发时都会被破坏,必须重新创建。他们不会持有你给他们的价值。这样做不是个好主意。动态创建控件只需几个步骤;你错过了很多步骤。看答案。如果您有特定的问题,请回来再问。在asp.net中使用动态控件是一个坏主意,因为控件在每次回发时都会被破坏,必须重新创建。他们不会持有你给他们的价值。这样做不是个好主意。动态创建控件只需几个步骤;你错过了很多步骤。看答案。如果您有特定的问题,请返回并再次询问。它会引发异常详细信息:System.NullReferenceException:Object reference未设置为对象的实例。@joyoares对于延迟,我感到非常抱歉。我用来测试的代码附在这本书上,它可以正常工作。请仔细检查。它抛出异常详细信息:System.NullReferenceException:Object reference未设置为对象的实例。@joyoares对于延迟,我真的很抱歉。我用来测试的代码附在这本书上,它可以正常工作。请检查一下。