Add array of controls to an aspx page

后端 未结 5 1994
暗喜
暗喜 2021-01-06 08:13

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)

5条回答
  •  时光说笑
    2021-01-06 08:28

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

    
    
    

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

提交回复
热议问题