单击按钮添加多个DropDownLists

使用ASP.NET(使用C#)和jQuery的组合,我尝试使用按钮创建DropDownList 。 这些DropDownList将填充SQL查询。

我遇到困难的部分是只使用按钮点击添加多个。

如果用户单击该按钮5次,则应创建另外5个DropDownList并使用相同的数据填充。 理想情况下,每个DropDownList都包含在表的新行中,或以类似的方式组织。

我不想在JavaScript中进行任何SQL连接。 有什么建议吗?

编辑:

我尝试使用 ,然后使用它来添加DropDownList 。 我无法弄清楚如何使用此方法并添加多个,但:

  protected void Add_Dropdownlist() { DropDownList DropDownList1 = new DropDownList(); PlaceHolder1.Controls.Add(DropDownList1); } 

我已经尝试使用jQuery来复制原始的DropDownList 。 我遇到的问题是,如果它是 ,我无法弄清楚如何从jQuery获取原始的DropDownList 。 如果我改为将其改为 ,我无法弄清楚如何用ASP(服务器)端的SQL数据填充它。

     function copy_Dropdownlist() { newDropdownlist = jQuey.extend({}, DropDownList2); } 

简而言之,我走了几条路,但我认为它们中的任何一条都不对。 我是ASP.NET(谷歌教育)的新手,我认为问题在于链接客户端jQuery和服务器端ASP.NET。 谢谢您的帮助。

(这是参考你的方法。我实际上将使用 ,因为这里似乎更合适)

看起来你遇到的问题是DropDownList不会在回发中持久存在。 因此,每次创建一个新的并将其添加到PlaceHolder ,您仍然只能得到一个。

要解决此问题,您需要跟踪已创建的内容。 在Page_Load ,您想为DropDownList创建一个存储容器(我为此使用List<> )并将其保存在用户的“ Session ”中。 注意!Page.IsPostBack ; 你只想创建一次(第一次加载页面,而不是每次你PostBack):

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List DDLList = new List(); Session["DDLs"] = DDLList; } } 

现在,当您单击按钮时,您可以

  • Session变量中获取当前存在的DropDownList (如果有),
  • 添加此按钮的新按钮单击该列表
  • 将所有DropDownList添加到Panel
  • 并将更新后的列表保存回Session变量。

像这样:

 protected void Button1_Click(object sender, EventArgs e) { DropDownList newDropDown = new DropDownList(); List existingDropDowns = (List)Session["DDLs"]; existingDropDowns.Add(newDropDown); foreach (DropDownList dropdown in existingDropDowns) { Panel1.Controls.Add(dropdown); } Session["DDLs"] = existingDropDowns; } 

我不确切地知道你想要完成什么,但这应该有希望让你开始。

@ jadarnel27

非常感谢你的帮助,它让我到了我需要去的地方。 我不得不做更多的工作来处理选择更改的post。 这就是我最终的结果。

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { List DDLList = new List(); Session["DDLs"] = DDLList; } else { List existingDropDowns = (List)Session["DDLs"]; //Add all existing DropDownLists to Panel foreach (DropDownList dropdown in existingDropDowns) { Panel1.Controls.Add(dropdown); dropdown.AutoPostBack=true; Panel1.Controls.Add(new LiteralControl("
")); } Session["DDLs"] = existingDropDowns; } } protected void Button_Click(Object sender, EventArgs e) { List
existingDropDowns = (List)Session["DDLs"]; DropDownList newDropDown = new DropDownList(); newDropDown.ID = "DDL" + existingDropDowns.Count.ToString(); //Bind DropDownList to SQL Table Populate_List("mySQLTable", newDropDown, ""); existingDropDowns.Add(newDropDown); //Add only new DropDownList to Panel Panel1.Controls.Add(newDropDown); newDropDown.AutoPostBack=true; Panel1.Controls.Add(new LiteralControl("
")); Session["DDLs"] = existingDropDowns; } protected void clickSubmit(Object sender, EventArgs e) { List
existingDropDowns = (List)Session["DDLs"]; foreach (DropDownList dropdown in existingDropDowns) { //Insert each DropDownList selected value into SQL Table ETS.Core.Db.Database.ExecuteNoLog("INSERT INTO myNewTable (id,text) VALUES ('" + dropdown.SelectedValue + "', '" + dropdown.SelectedItem + "')"); } }

再次感谢。