Let’s say you need to create web form with dynamic number of input fields. In this article i will show you how to create input controls at run time.
Suppose you have a web form that contains some design time controls.:
One more thing that page_load event will rebuild all the control field every time the page will be loaded. When we create a new control to a page, the control is not automatically retained between form posts. The control is not preserved in the page’s view state but any entered text in to dynamically generated form fields is automatically preserved in view state.
In the following example- We have a student name registration web form that contains a ‘Add new student’ button. User can insert a new text input field on the web form by clicking the button.
Designer page’s code:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <span style="font-size:13">Class X</span> </br> </br> <asp:Button ID="Button1" runat="server" Text="Add New Student" onclick="Button1_Click" /> </div> </form> </body> </html>
 :
Code page:
using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == true) { if (ViewState["Count"] != null) { arrViewState = (int)ViewState["Count"]; } CreatePostbackControls(arrViewState); } } private void CreatePostbackControls(int _arrViewState) { if( _arrViewState != null ) { for (int j = 1; j <= _arrViewState; j++) { string _id = j.ToString(); Label lblheading = new Label(); lblheading.ID = "lblheading_" + _id + "_"; lblheading.Text = "Enter the student name:"; lblheading.Width = 160; lblheading.Attributes.Add("style", "color:#015D84;font-weight:bold;font-size:12px;padding:10px;"); lblheading.EnableViewState = true; form1.Controls.Add(lblheading); TextBox txtfname = new TextBox(); txtfname.ID = "TextBox_" + _id + "_"; txtfname.Width = 160; txtfname.EnableViewState = true; form1.Controls.Add(txtfname); form1.Controls.Add(new LiteralControl("<br/>")); } } } private int arrViewState = 0; protected void Button1_Click(object sender, EventArgs e) { arrViewState =arrViewState+1; string _id = arrViewState.ToString(); Label lblheading = new Label(); lblheading.ID = "lblheading_" + _id + "_"; lblheading.Text = "Enter the student name:"; lblheading.Width = 160; lblheading.Attributes.Add("style", "color:#015D84;font-weight:bold;font-size:12px;padding:10px;"); lblheading.EnableViewState = true; form1.Controls.Add(lblheading); TextBox txtfname = new TextBox(); txtfname.ID = "TextBox_" + _id + "_"; txtfname.Width = 160; txtfname.EnableViewState = true; form1.Controls.Add(txtfname); ViewState["Count"] = arrViewState; form1.Controls.Add(new LiteralControl("<br/>")); } }
Can you please post the insert query for the previous code
This is a dynamic form, It will not be inserted link columns and values.
For saving this form you need to create table for saving all the values of the form with all the values of labels.
same like inserting multiple products for one purchase order.
This code is only for creating controls
i need code for inserting these values in a database table
please help
Insert another button. On this button click event: get the values of the all textboxes and create the query at runtime.
That i know…but am unable to find the controls
i tried with Find controls ..its not working
try like this:
foreach (Control ctrl in form1.Controls)
{
if (ctrl.ID != null)
{
if (ctrl.ID.ToString().StartsWith(“TextBox_”))
{
}
}
}
If possible post the code