Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to play with ASP.NET's custom controls, you have to play by its rule and its picky! When you start to play with the OnPreRender in a custom control, you know you're on the wrong track 90% of the time.</p> <p>Generally, the best way to use the ViewState is to declare a property backed up by it, just like the standard ASP.NET controls do (.NET Reflector has been my teacher for years!). This way, it will be read and saved naturally during the event's lifecycle.</p> <p>Here is a code that seems to do what you want, quite naturally, without any trick:</p> <pre><code>public class AddManyControl : CompositeControl { private void AddControl(int index) { var div = new HtmlGenericControl("div"); var textBox = new TextBox(); textBox.ID = "tb" + index; div.Controls.Add(textBox); Controls.AddAt(index, div); } protected override void CreateChildControls() { for (int i = 0; i &lt; ControlsCount; i++) { AddControl(i); } var btnAdd = new Button(); btnAdd.ID = "Add"; btnAdd.Text = "Add text box"; btnAdd.Click += new EventHandler(btnAdd_Click); Controls.Add(btnAdd); var btnPostBack = new Button(); btnPostBack.ID = "PostBack"; btnPostBack.Text = "Do PostBack"; Controls.Add(btnPostBack); } private int ControlsCount { get { object o = ViewState["ControlCount"]; if (o != null) return (int)o; return 0; } set { ViewState["ControlCount"] = value; } } void btnAdd_Click(object sender, EventArgs e) { int count = ControlsCount; AddControl(count); ControlsCount = count + 1; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload