Add array of controls to an aspx page?

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

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 ListItem3; ... DropDownList lists = new DropDownList { FruitDropDown0 ,FruitDropDown1 ,...}; foreach(DropDownList list in lists) { list.Items. AddRange(items); list.DataBind(); } }.

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 FindControl("FruitDropDown" + x); ddlFruit.Items. AddRange(itemsx); } You can also create them dynamically: for (int x = 0; x AddRange(itemsx); 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.

– George Oct 12 '11 at 22:04 You can use FindControls also on dynamically created controls. But as you can see in my example, you can do both in one loop without findcontrol because you already have a reference of the newly created control there. – Tim Schmelter Oct 12 '11 at 22:06 If I use the second example to create them dynamically, I understand they will be added to the FruitPanel control on the aspx page.

However, how does this handle positioning and layout? I want to create a few dynamic textboxes/dropdowns and added them in a single row. How will I control the layout by just calling the add method?

Do I have to set the position manually? I could add 20 controls to the page, but that seems like a bad way to do it - how could I add those in a loop on the page to keep the layout consistent? – George Oct 12 '11 at 22:59 The positioning and layout of the FruitPanel should be controlled by CSS just as for the DropDownList.

If you need more control over the layout of each DropDown you should show us a model, then you maybe should use other Container controls like Repeater or GridView what makes also the dynamic creation of the controls redundant. – Tim Schmelter Oct 12 '11 at 6:59.

From your question it looks like you are trying dynamically render controls on the screen Here is a good article on doing this.

You should look into dynamically creating controls. support.microsoft.com/kb/317794.

Like @Mark Cidade says your best approach would be to create the controls in the code behind. They are just classes after all. The simplest approach would be to put a control such as a placeHolder control on in the markup and then create a collection of dropdown lists in a loop, assinging each one a unique id like @Mark Cidade says.

From there it's a matter of adding them as child controls to the placeHolder or if you want them directly on the page you can add them to the page controls collection.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions