添加控件数组的aspx页面 [英] Add array of controls to an aspx page

查看:98
本文介绍了添加控件数组的aspx页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图控件数组添加到一个aspx页面(C#code后面)。这些都是类似的控制(为单独的字段的搜索条件,但将具有相同的值)。下拉控制值在Page_Load方法当前设置,如下所示:

I'm trying to add an array of controls to an aspx page (C# code behind). These are the similar controls (search criteria for separate fields but will have the same values). The dropdown control values are currently set in the Page_Load method, like this:

protected void Page_Load(object sender, EventArgs e) {
  ListItem[] items = new ListItem[3];
  //these values are actually set by a database query
  items[0] = new ListItem('Apple', 'Apple');
  items[1] = new ListItem('Orange', 'Orange');
  items[2] = new ListItem('Banana', 'Banana');

  FruitDropDown.Items.AddRange(items);
  FruitDropDown.DataBind();
}

然后在页面上我有这样一个控件:

then on the page I have a control like:

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

这工作得很好,并填充了控制,但我想现在创建这些的下拉列表中的数组 - 将有其中的20个,所以我不想在Page_Load方法来创建它的20倍。我应该在列表中添加下拉人口,然后像做:

This works fine and populates the control, but I want to now create an array of those dropdowns - there will be 20 of them, so I don't want to create it 20 times in the Page_Load method. Should I add the dropdown population in a list and then do something like:

<asp:DropDownList ID="FruitDropDown[0]" runat="server"/>
<asp:DropDownList ID="FruitDropDown[1]" runat="server"/>

我将如何对其进行标记在页面上,然后可以访问控制器中的这些价值观?我可以做这样的事情,如果我已经添加这些命名控件添加到aspx页面?

How would I label them on the page and then be able to access those values in the controller? Could I do something like this if I've already added those named controls to the aspx page?

protected void Page_Load(object sender, EventArgs e) {
  ListItem[] items = new ListItem[3];
  //these values are actually set by a database query
  items[0] = new ListItem('Apple', 'Apple');
  items[1] = new ListItem('Orange', 'Orange');
  items[2] = new ListItem('Banana', 'Banana');

  for (int x = 0; x < 20; x++) {
    FruitDropDown[x].Items.AddRange(items);
    FruitDropDown[x].DataBind();
}

这是否也需要复制和粘贴aspx页面上的控件的20倍?如果扩大到100呢?我可以在aspx页面上添加那些在一个循环?

Does this also require copying and pasting the controls on the aspx page 20 times? What if it expands to 100? Can I add those in a loop on the aspx page?

推荐答案

控制。的FindControl 是你在找什么。你可以用它在任何控制(如页面本身)通过他们的 NamingContainer 。把它们例如一个面板和使用的FindControl就可以了。

Control.FindControl is what you're looking for. You can use it on any Control(like Page itself) to find controls via their NamingContainer. Put them e.g. in a Panel and use FindControl on it.

for (int x = 0; x < 20; x++) {
    DropDownList ddlFruit = (DropDownList)FruitPanel.FindControl("FruitDropDown" + x);
    ddlFruit.Items.AddRange(items[x]);    
}

您还可以动态创建它们:

You can also create them dynamically:

for (int x = 0; x < 20; x++) {
   DropDownList ddlFruit = new DropDownList();
   ddlFruit.ID = "FruitDropDown" + x
   ddlFruit.Items.AddRange(items[x]);  
   FruitPanel.Controls.Add(ddlFruit); 
}

您必须重新上在Page_Load中最新的具有相同ID的所有回传动态创建控件之前,确保ViewState的正确装入,事件被触发。

You must recreate dynamically created controls on every postback at the latest in Page_Load with the same ID as before to ensure that ViewState is loaded correctly and events are triggered.

这篇关于添加控件数组的aspx页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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