banner



How To Dynamically Create Controls In Asp Net With C#

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

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.

How To Dynamically Create Controls In Asp Net With C#

Source: https://www.c-sharpcorner.com/article/how-to-make-dynamic-control-in-asp-net-using-panels/

Posted by: linseymarban.blogspot.com

0 Response to "How To Dynamically Create Controls In Asp Net With C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel