Dynamically Add Server Controls in ASP.Net 2

Add your dynamic control to your control structure before setting properties.

private partial class _Default : System.Web.UI.Page
...
protected void Page_Load (object sender, EventArgs e){
   TextBox t = new TextBox();
   this.myForm.Controls.Add(t); //or myPanel.Controls.Add(t) etc...
   if(!Page.IsPostBack){
      t.Text = “shenanigans“;
      // other default stuff
   }
   t.ID = “txt“ + GetFancyDynamicControlName();
}

Print | posted on Monday, March 12, 2007 9:45 PM