添加多个DropDownLists通过单击按钮 [英] Adding multiple DropDownLists with a button click

查看:105
本文介绍了添加多个DropDownLists通过单击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.NET的结合(用C#)和jQuery,我尝试创建的DropDownList s的一个按钮。这些的DropDownList 被带一个SQL查询来填充。

Using a combination of ASP.NET (with C#) and jQuery, I'm try to create DropDownLists with a button. These DropDownLists will be populated with a SQL query.

我在与只用按一下按钮的添加多个困难的一部分。

The part I'm having difficulty with is adding more than one using just the button click.

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

If the user clicks the button 5 times, 5 more DropDownLists should be created and populated with the same data. Ideally, each DropDownList is contained in a new row of a table, or organized in a similar fashion.

我不想做在JavaScript中的SQL连接。与此有何建议?

I do not want to do any SQL connections in JavaScript. Any advice with this?

编辑:

我已经使用试过< ASP:占位符> ,然后使用添加的DropDownList 。我无法弄清楚如何使用此方法,并添加多个,虽然:

I've tried using an <asp:PlaceHolder>, and then using that to add a DropDownList. I could not figure out how to use this method and add more than one, though.:

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

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

我使用jQuery复制原始的DropDownList 审判。我有这个问题,是我无法弄清楚如何获得原始的DropDownList 从jQuery的,如果它是一个&LT; ASP:DropDownList的&GT ; 。如果我把它改为&LT;选择方式&gt; 相反,我无法弄清楚如何从ASP(服务器)端SQL数据填充

I've tried using jQuery to copy the original DropDownList. The problem I had, was that I couldn't figure out how to get the the original DropDownList from jQuery if it was an <asp:DropDownList>. If I changed it to a <select> instead, I could not figure out how to populate it with SQL data from the ASP (server) side.

<select id="DropDownList1">
  <option value="-1"></option>
</select>

<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>

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

总之,我已经走了下来几条路径,但我不认为任何人都正确。我是新来ASP.NET(谷歌教育),我认为问题出在连接客户端jQuery和服务器端ASP.NET。感谢您的帮助。

In short, I've gone down several paths, but I don't think any of them are right. I'm new to ASP.NET (google education) and I think the problems lies in linking the client side jQuery and the server side ASP.NET. Thanks for the help.

推荐答案

(这是在参考你的&LT; ASP:占位符&GT; 的办法,我真的要使用一个&LT; ASP:面板&gt; ,因为它似乎更适合这里)

(This is in reference to your <asp:PlaceHolder> approach. I'm actually going to use an <asp:Panel>, as it seems more appropriate here)

它看起来像你正在运行到的问题是,的DropDownList ,则不能在回传仍然存在。所以,每次你创建一个新的,并把它添加到你的占位符,你还是只有一个结束。

It looks like the problem you're running into is that the DropDownLists are not persisted across postbacks. So, each time you create a new one and add it to your PlaceHolder, you still only end up with one.

要解决这个问题,你需要跟踪你已经创建的那些的。在你的的Page_Load ,你要为你的的DropDownList S(我用的是列表&LT;&GT; 本)并将其保存在用户的会话。注意 Page.IsPostBack !;您只需要一次创建这个(首次加载页面,你不回发的每一次):

To resolve this, you need to keep track of the ones you've already created. In your Page_Load, you want to create a storage container for your DropDownLists (I use a List<> for this) and save it in the user's "Session". Notice the !Page.IsPostBack; you only want to create this once (the first time the page loads, not each time you PostBack):

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

现在,当您单击该按钮,就可以

Now when you click the button, you can


  • 获取现有的DropDownList S(如果有的话)从会话变量,

  • 添加单击此按钮新建一个到该列表

  • 所有的的DropDownList ■添加到面板

  • 并保存更新列表回到你的会话变量。

  • Get the currently existing DropDownLists (if any) from your Session variable,
  • Add the new one for this button click to that list
  • Add all of your DropDownLists to the Panel,
  • And save the updated list back to your Session variable.

这样的:

protected void Button1_Click(object sender, EventArgs e)
{
    DropDownList newDropDown = new DropDownList();
    List<DropDownList> existingDropDowns = (List<DropDownList>)Session["DDLs"];
    existingDropDowns.Add(newDropDown);

    foreach (DropDownList dropdown in existingDropDowns)
    {
        Panel1.Controls.Add(dropdown);
    }

    Session["DDLs"] = existingDropDowns;
}

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

I don't know exactly what you're trying to accomplish, but this should hopefully get you started.

这篇关于添加多个DropDownLists通过单击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆