Create Dynamic ASP.NET Server Control with Theme (Template) Style

Introduction
 
This post will show you how to add and dynamically create ASP.NET Server Controls and place the controls into an HTML DIV server tag. Create your ASP.NET web application [project] and integrate your required theme(template). As per your requirement, create a web form and design page layout and create a container div with id="myFormContainer" and div as a server control-runat="server". In this div going to add child div, label, textbox, button, etc. controls dynamically.

Create Dynamic ASP.NET Server Control

All controls list retrieved from "Controls" json object (List). You can create a json file and deserialize json object with C# class object, easily create number of controls dynamically with required attributes, e.g., Id, text, etc. You can also try to control list retrieved from database table.

Classes, Methods and Events

Step 1: Create Basic Classes

  1. public class Control  
  2. {  
  3.     public string ID { getset; }  
  4.     public string LabelText { getset; }  
  5. }  
  6. public class ControlList  
  7. {  
  8.     public List<control> Controls { getset; }  
  9. }  

Step 2: Controls List (Get from json Object or Database Table) 

  1. // create the div to add form elements from a controls list  
  2. string json = @"{Controls:[{ID:'UserName', LabelText:'User Name'},  
  3. {ID:'EmailId', LabelText:'Email Address'},{ID:'Password', LabelText:'Password'},  
  4. {ID:'Phone', LabelText:'Phone Number'}]}";  
  5. var controls = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<controllist>(json);  

Step 3: Create Method to Bind Controls

  1. /// <summary>  
  2. /// Create & Add Controls to the container div  
  3. /// </summary>  
  4. /// <param name="controls"></param>  
  5. private void CreateControls(ControlList controls)  
  6. {  
  7.     foreach (var control in controls.Controls)  
  8.     {  
  9.         //Create Group Container Div  
  10.         HtmlGenericControl div = new HtmlGenericControl("div");  
  11.         div.Attributes.Add("class""form-group");  
  12.         // create label and add to the div            
  13.         div.Controls.Add(new Label() { Text = control.LabelText,  
  14.         AssociatedControlID = control.ID, CssClass = "col-md-2 control-label" });  
  15.         //create the div to add controls eg. textbox, checkbox etc.  
  16.         HtmlGenericControl divInner = new HtmlGenericControl("div");  
  17.         divInner.Attributes.Add("class""col-md-10");  
  18.         //Create TextBox  
  19.         divInner.Controls.Add(new TextBox() { ID = control.ID, CssClass = "form-control" });  
  20.         //Create Validator  
  21.         divInner.Controls.Add(new RequiredFieldValidator()   
  22.         { ControlToValidate = control.ID, CssClass = "text-danger",   
  23.         ErrorMessage = "The user name field is required." });  
  24.         div.Controls.Add(divInner);  
  25.         Container.Controls.Add(div);  
  26.     }  
  27.     //create button with event and add to the div  
  28.     var button = new Button { ID = "btnClick", Text = "Create" };  
  29.     button.Click += btnClick_OnClick;  
  30.     Container.Controls.Add(button);  
  31. }  

Step 4: Call CreateControl() Method on OnInit Event

  1. /// <summary>  
  2. /// Load Controls on OnInit event  
  3. /// </summary>  
  4. /// <param name="e"></param>  
  5. override protected void OnInit(EventArgs e)  
  6. {  
  7.     // create the div to add form elements from a controls list  
  8.     string json = @"{Controls:[{ID:'UserName', LabelText:'User Name'},  
  9.     {ID:'EmailId', LabelText:'Email Address'},{ID:'Password', LabelText:'Password'},  
  10.     {ID:'Phone', LabelText:'Phone Number'}]}";  
  11.     var controls = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<controllist>(json);  
  12.     CreateControls(controls); // Method with controls list param  
  13. }