How To Make Dynamic Control In ASP.NET Using Panels

In this tutorial, we will learn about dynamic control and how to add it in ASP.NET application using C#. The objective is simple; we will just add some basic controls in runtime. We will use some panels to adjust our dynamic control based on the control container.

Well, this tutorial is not as worthy as it should be because generally the objectives that we include here are quite simple and also in real development, the dynamic control that we are running is never placed like that. The so called dynamic is actually used when we need the resources, and we don’t want to use the whole IDE, at that time such dynamic controls are quite useful.

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an Empty Website. Give a suitable name [dynamic_demo].

Step 2

In Solution Explorer, you get your empty website. Add a web form –

For Web Form

dynamic _demo (Your Empty Website) -> Right Click -> Add New Item -> Web Form. Name it as -> dynamic _demo.aspx.

DESIGN CHAMBER

Step 3

Open your dynamic_demo.aspx, and drag and drop the table. Inside one of the td  tags, you have to place checkboxlist, for prompting the user to what controls they want to generate. So, we had taken almost 5 controls as – Textbox, button, label, dropdown, and radiobutton. After that, we will place one textbox to prompt the user to choose how many controls they want to add. Then, a simple button control is placed to generate the control dynamically. Your design will look like the below figure.


Here, after every control, we have placed a panel, so that the dynamic control is placed at this appropriate place. We hope that for the above design, we do not have to share the whole td and tr tag code because it is a lengthy table. For simplicity, we have shared our code too, you can refer that in case any trouble occurs.

CODE CHAMBER

Step 4

Open dynamic_demo.aspx.cs file to write our code. Here, we will  first check what controls the user wants to generate. After getting that, we will count how many controls he wants. Suppose, he puts 3, then every control will make 3 dynamic controls.

dynamic_demo.aspx.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.IO;  
  8.   
  9. public partial class dynamic_control : System.Web.UI.Page  
  10. {  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.           
  14.     }  
  15.     protected void Button1_Click(object sender, EventArgs e)  
  16.     {  
  17.   
  18.         int count = Convert.ToInt32(Txtcontrol.Text);  
  19.         foreach (ListItem lst in CheckBoxList1.Items)  
  20.         {  
  21.             if (lst.Selected)  
  22.             {  
  23.                 if (lst.Value =="TextBox")  
  24.                 {  
  25.                     for (int i = 1; i <= count; i++)  
  26.                     {  
  27.                         TextBox txt = new TextBox();                       
  28.                         txt.Text = "TextBox" + i.ToString();                         
  29.                         Panel1.Controls.Add(txt);  
  30.                         Setprompt(txt);                                                                   
  31.                     }  
  32.                 }  
  33.                 if (lst.Value == "RadioButton")  
  34.                 {  
  35.                     for (int i = 1; i <= count; i++)  
  36.                     {  
  37.                         RadioButton rdb = new RadioButton();  
  38.                         rdb.Text = "RadioButton" + i.ToString();  
  39.                         Panel2.Controls.Add(rdb);  
  40.                     }  
  41.                 }  
  42.                 if (lst.Value == "DropDown")  
  43.                 {  
  44.                     for (int i = 1; i <= count; i++)  
  45.                     {  
  46.                         DropDownList dbr = new DropDownList();  
  47.                         dbr.Text = "DropDown" + i.ToString();  
  48.                         Panel3.Controls.Add(dbr);  
  49.                         dbr.Items.Add("--Select--");  
  50.                         
  51.                     }  
  52.                 }  
  53.   
  54.                 if (lst.Value == "Button")  
  55.                 {  
  56.                     for (int i = 1; i <= count; i++)  
  57.                     {  
  58.                         Button btn = new Button();  
  59.                         btn.Text = "Button" + i.ToString();  
  60.                         Panel4.Controls.Add(btn);  
  61.                     }  
  62.                 }  
  63.   
  64.                 if (lst.Value == "Label")  
  65.                 {  
  66.                     for (int i = 1; i <= count; i++)  
  67.                     {  
  68.                         Label lbl = new Label();  
  69.                         lbl.Text = "Label" + i.ToString();  
  70.                         Panel5.Controls.Add(lbl);  
  71.                     }  
  72.                 }  
  73.   
  74.                 if (lst.Value == "CheckBox")  
  75.                 {  
  76.                     for (int i = 1; i <= count; i++)  
  77.                     {  
  78.                         CheckBox chk = new CheckBox();  
  79.                         chk.Text = "CheckBox" + i.ToString();  
  80.                         Panel6.Controls.Add(chk);  
  81.                     }  
  82.                 }  
  83.             }  
  84.         }        
  85.     }  
  86.     public string ControlRenderer(Control control)  
  87.     {  
  88.         StringWriter writer = new StringWriter();  
  89.         control.RenderControl(new HtmlTextWriter(writer));  
  90.         return writer.ToString();  
  91.     }  
  92.     private void Setprompt(TextBox txt)  
  93.     {  
  94.         string onFocusAction = "if (this.value == \"{0}\") {{ this.value = \"\"; this.style.color = \"black\"; }} ";  
  95.         string onBlurAction = " if (this.value == \"\") {{ this.value = \"{0}\"; this.style.color = \"gray\"; }} else this.style.color = \"black\";";  
  96.         onBlurAction = string.Format(onBlurAction, txt.Text);  
  97.         onFocusAction = string.Format(onFocusAction, txt.Text);  
  98.         txt.Attributes["onblur"] = onBlurAction;  
  99.         txt.Attributes["onfocus"] = onFocusAction;  
  100.   
  101.     }  
  102.   
  103.   
  104. }   

The last two methods are specifically for the textboxes. You will see that when you generate the textbox dynamically, there will be a default text written inside the Textbox, something like Textbox1, Textbox 2 and so on based on the control you had generated. To remove that, you have to click inside the textbox and then use backspace key.

The approach is tedious, so we have used these two methods which will automatically remove the default text when you focus on the control.

OUTPUT CHAMBER

OUTPUT

Hope you like this. Have a good day. Thank you for reading.