In this article, we will learn how to create a dynamic form in ASP.NET. Here, dynamic means that we will create the controls at run time.

Suppose someone asks you to create a form in ASP.NET. Naturally, your first response might be, “It’s easy!”—you would go to an ASPX page, drag the required controls from the toolbox, and the form would be ready in no time. However, what if you’re told not to use the toolbox or even the ASPX page to add controls? Instead, you are required to dynamically create the form and its controls at runtime.

Is this possible? Yes, absolutely!

Understand the Requirement

Before jumping into code, always ensure you clearly understand the requirement. Without a thorough understanding, perfect implementation becomes difficult. You are required to generate a dynamic form at runtime with specific form fields and control types, and you must be able to retrieve the user input values from these dynamically created controls.

How to Create Dynamic Form in ASP.NET and Retrieve Input Values | TechAid24

In the above image, I created a function according to our needs. In this function, I created a data table with three columns (Fieldname, Fieldtype, and Fieldvalue). Let’s create another function in which we will create the ASP.NET controls that will return the DataTable according to the current function.

Function to create dynamic form in ASP.NET
I created the above function for creating the dynamic controls. We called the function in which we created the data table with fieldname, fieldtype, etc. Based on that function, we used a loop and got the fieldname and fieldtype one by one and made the check. 
 
Then, we went into this block and created a control from the class Textbox by giving it attributes, like id value dynamically, nothing static, and we will add that in the HtmlGenericControl. We created table row and table data, the same way we did for other controls. At last, we checked if the for loop is running for the last count. Then, we added the submit button, client click event. After this, we added it to the Generic control.
 
Now, it is time to run it and see the output. We call this function on the Page_Load event, outside IsPostBack property; because, we need to get the output also. If we call this function within IsPostBack, we can’t find the controls on the page.
Page load event

So, hit the F5 button and see the output. Our form is ready. If we have taken the controls on aspx page, it will work fine. So, let’s check the aspx page.

We just have a placeholder where we added controls dynamically in the code behind. Now, the form is ready.

Let’s move to the next requirement, to read the data from these controls. Since we have created these controls dynamically, we have to read the data from these controls in the same way. I created a new function to read the data from these fields. I tried to make this function as short and as dynamic as I could.

Function to Retrieve Input Values

We created a function in which we call the same function where we have the fieldname and fieldtype. Through that, we would find the control in the placeholder with the dynamic ID and create a data table.

After finding the control, we added the data to this data table. Now, in this for loop, you can save this data table into your database. I haven’t done it yet, but I will show you how the data I put in the form gets added in this data table. 

Save button event

Now, enable break breakpoint in this Save function and see after the for loop result in the data table if you get the correct data or not. When we fill the form and hit the submit button, the values are stored in the data table. You can save these values in the database.

I haven’t saved the values in the database, just created the dynamic form to show you how we can get the values from these controls. That’s it for now.

Leave a Comment