Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add array of controls to an aspx page

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"/>

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"/>

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();
}

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?

like image 639
George Avatar asked Feb 23 '23 17:02

George


2 Answers

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); 
}

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.

like image 196
Tim Schmelter Avatar answered Mar 04 '23 12:03

Tim Schmelter


You can give them valid IDs and put them all in an array yourself:

<asp:DropDownList ID="FruitDropDown0" runat="server"/>
<asp:DropDownList ID="FruitDropDown1" runat="server"/>

protected void Page_Load(object sender, EventArgs e) {
   ListItem[] items = new ListItem[3];
   ...

   DropDownList[] lists = new DropDownList[] { FruitDropDown0
                                              ,FruitDropDown1
                                              ,...};

   foreach(DropDownList list in lists) {
      list.Items.AddRange(items);
      list.DataBind();
   }
}
like image 39
Mark Cidade Avatar answered Mar 04 '23 12:03

Mark Cidade