In this article, we will learn how we can create a dynamic form in ASP.NET. Here, dynamic means that we will create the controls at run time.
Introduction
Suppose someone asks you to create a form in ASP.NET. You will say, "It's easy". You will go to an aspx page, drag the controls from toolbox, and the form is ready. But, the person doesn't want you to drag the controls from the toolbox or write in aspx page. Our requirement is to create the controls at run time. So, is that doable? Yes! It is. We can do this in two ways. I will describe here only one way. However, you will also get a hint of the second. So, let's start the code.
Suppose someone asks you to create a form in ASP.NET. You will say, "It's easy". You will go to an aspx page, drag the controls from toolbox, and the form is ready. But, the person doesn't want you to drag the controls from the toolbox or write in aspx page. Our requirement is to create the controls at run time. So, is that doable? Yes! It is. We can do this in two ways. I will describe here only one way. However, you will also get a hint of the second. So, let's start the code.
First, look into the requirement.
Suppose, we have a requirement where we need to generate the following form fields and their corresponding field types. This, we need to do dynamically at the run time and have to get the values from them. So, our requirement is clear. Always understand the requirement first because without knowing what exactly we need to do, we can't code perfectly.
Requirement


From the above image, we know what field we will have in the form and what type of controls will be needed for those fields. For example, we have Firstname which will take Textbox.
So, let's start doing the code.

In the above image, I created a function according to our need. In this function, I created a data table with three columns (Fieldname, Fieldtype, and Fieldvalue). So now, our frame is ready. Let's fit it into the form. Let's create another function in which we will create the ASP.NET controls which will return the datatable according to the current function.

In the above image, I created a function according to our need. In this function, I created a data table with three columns (Fieldname, Fieldtype, and Fieldvalue). So now, our frame is ready. Let's fit it into the form. Let's create another function in which we will create the ASP.NET controls which will return the datatable according to the current function.
Dynamic controls function
- public void CreateDynamicControls()
- {
- DataTable dt = new DataTable();
- dt = CustomFields(); //calling the function which describe the fieldname and fieldtype
- if(dt.Rows.Count>0)
- {
- for(Int32 i=0;i<dt.Rows.Count;i++)
- {
- HtmlGenericControl tr = new HtmlGenericControl("tr");
- HtmlGenericControl td = new HtmlGenericControl("td");
- HtmlGenericControl td1 = new HtmlGenericControl("td");
- String FieldName=Convert.ToString(dt.Rows[i]["FieldName"]);
- String FieldType = Convert.ToString(dt.Rows[i]["FieldType"]);
- String FieldValue = Convert.ToString(dt.Rows[i]["FieldValue"]);
- Label lbcustomename = new Label();
- lbcustomename.ID = "lb" + FieldName;
- lbcustomename.Text = FieldName;
- td.Controls.Add(lbcustomename);
- tr.Controls.Add(td);
- if (FieldType.ToLower().Trim()=="textbox")
- {
- TextBox txtcustombox = new TextBox();
- txtcustombox.ID = "txt" + FieldName;
- txtcustombox.Text = FieldValue;
- td1.Controls.Add(txtcustombox);
- }
- else if(FieldType.ToLower().Trim() == "checkbox")
- {
- CheckBox chkbox = new CheckBox();
- chkbox.ID = "chk" + FieldName;
- if(FieldValue=="1")
- {
- chkbox.Checked = true;
- }
- else
- {
- chkbox.Checked = false;
- }
- td1.Controls.Add(chkbox);
- }
- else if (FieldType.ToLower().Trim() == "radiobutton")
- {
- RadioButtonList rbnlst = new RadioButtonList();
- rbnlst.ID = "rbnlst" + FieldName;
- rbnlst.Items.Add(new ListItem("Male","1"));
- rbnlst.Items.Add(new ListItem("Female", "2"));
- if(FieldValue!=String.Empty)
- {
- rbnlst.SelectedValue = FieldValue;
- }
- else
- {
- rbnlst.SelectedValue = "1";
- }
- rbnlst.RepeatDirection =RepeatDirection.Horizontal;
- td1.Controls.Add(rbnlst);
- }
- else if(FieldType.ToLower().Trim() == "dropdownlist")
- {
- DropDownList ddllst = new DropDownList();
- ddllst.ID = "ddl" + FieldName;
- ddllst.Items.Add(new ListItem("Select", "0"));
- if(FieldName.ToLower().Trim()=="state")
- {
- ddllst.Items.Add(new ListItem("Alabama", "AL"));
- ddllst.Items.Add(new ListItem("Alaska", "AK"));
- ddllst.Items.Add(new ListItem("Arizona", "AZ"));
- ddllst.Items.Add(new ListItem("California", "CA"));
- ddllst.Items.Add(new ListItem("New York", "NY"));
- }
- else if(FieldName.ToLower().Trim() == "job")
- {
- ddllst.Items.Add(new ListItem("Developer", "1"));
- ddllst.Items.Add(new ListItem("Tester", "2"));
- }
- if (FieldValue != String.Empty)
- {
- ddllst.SelectedValue = FieldValue;
- }
- else
- {
- ddllst.SelectedValue = "0";
- }
- td1.Controls.Add(ddllst);
- }
- tr.Controls.Add(td1);
- placeholder.Controls.Add(tr);
- //Add button after last record
- if (i==dt.Rows.Count-1)
- {
- tr = new HtmlGenericControl("tr");
- td = new HtmlGenericControl("td");
- Button btnSubmit = new Button();
- btnSubmit.ID = "btnSubmit";
- btnSubmit.Click += btnsubmit_Click;
- btnSubmit.OnClientClick = "return ValidateForm();";
- btnSubmit.Text = "Submit";
- td.Controls.Add(btnSubmit);
- td.Attributes.Add("Colspan", "2");
- td.Attributes.Add("style", "text-align:center;");
- tr.Controls.Add(td);
- placeholder.Controls.Add(tr);
- }
- }
- }
- }






Parvez ChhapgarPosted Aug 5, 2022, 8:55 AM
Hi Sorry I am brushing up my asp.net skills after a long long time so excuse me for the stupid question. My Question is I am unable to create the functions as it gives me error for DataTable and String itself
Alicia SánchezPosted Apr 26, 2021, 8:18 AM
When you click the save button, the postback is executed and the dynamically generated controls no longer exist. How can i fix this?
pratik prabhakarPosted Apr 21, 2021, 5:13 AM
Please make an article on how to save data of dynamic created control in database. above article was very helpful to me but i am stuck at saving data in database
Charles NguyenPosted Jun 23, 2020, 12:40 PM
What if I wanted a textbox in-line with a dropdown list? How to do this?
Sunil BurliPosted Oct 23, 2019, 8:05 AM
Does anyone know how to get the same thing done for checkboxlist and radiobuttonlist.
Nori SouryPosted Oct 9, 2019, 10:41 AM
Hi Anil, upon button click save() is not run. Any workaround?
Aleksei WolffPosted Nov 9, 2018, 12:52 PM
Nice article. What is the placeholder control? It does not seem be available anymore in VS2017 under System.Web.UI.WebControls Any suggestions?
akash patniPosted Jul 16, 2018, 7:25 AM
Nice artical bro Can you provide Code for above example ?
Manish VadukulPosted Jun 26, 2018, 6:43 AM
Please can i have this code or project?
hitesh prajapatiPosted Feb 21, 2018, 7:56 PM
Hello bro, can you please help me.. i got the same requirement in mvc in my oraganization but i dont have any dynamic pattern.. currently i am feeling too much said because still i have not done this requirement ... i tried many ideas to implemented this thing like scaffolding, many other alfernative operation but yet it is not done.. kindly help...
hitesh prajapatiPosted Feb 21, 2018, 7:49 PM
Hello bro, thanks to provided very great smart dynamic functionality to reduced our time typed manual custom coding.. thanks to share it..
Bhargav MistriPosted Jun 20, 2017, 9:20 AM
Hello sir, i have saved but, but do u have any idea how i can show that in grid view with table format , Fielda name as column and value data as Row ?
Bhargav MistriPosted Jun 16, 2017, 1:40 AM
Hello, save event not work, i did all codes but when i click on save button in backend save event not call. how can i fix it ?
Bhargav MistriPosted Jun 12, 2017, 6:39 AM
How to download source code of this code ?
Patel SoniyaPosted Jun 1, 2017, 7:39 AM
ClientId is your table id filed
Gopi KrishnanPosted May 24, 2017, 9:30 AM
I have the Exception"Input string was not in a correct format.Couldn't store <__Page> in FormId Column. Expected type is Int32.." when try to store the ClientID how to fix it?
Patel SoniyaPosted Apr 25, 2017, 5:22 AM
It's Not Working in Is if (!IsPostBack) condition
Manav PandyaPosted Apr 17, 2017, 7:12 AM
Thanks for sharing ........
Muhammad ZafarPosted Feb 9, 2017, 10:22 AM
Nice article. I am just curious about validation on the basis of data types . Can you please share the link of article you mentioned about validation. or if you have developed complete on page can you share ??
Ronald seloanePosted Dec 30, 2016, 6:24 AM
Hi Anil Thakur, i like you concept of dynamic. i need more toturials for dynamics.
Vignesh ManiPosted Aug 4, 2016, 7:33 AM
Good
Humayun Kabir MamunPosted Aug 3, 2016, 3:27 PM
Nice, My question is What is the datatype of "ClientId" and how I will take value of "ClientId"?