Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
44 views1 page

Dynamically Adding TextBox

The document describes a method for dynamically adding text boxes and labels to a form in response to a button click. It includes a loop that adds controls based on a counter and maintains state information using unique IDs for each textbox and dropdown list. The total number of added controls is tracked using ViewState to ensure persistence across postbacks.

Uploaded by

Ali Shaukat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views1 page

Dynamically Adding TextBox

The document describes a method for dynamically adding text boxes and labels to a form in response to a button click. It includes a loop that adds controls based on a counter and maintains state information using unique IDs for each textbox and dropdown list. The total number of added controls is tracked using ViewState to ensure persistence across postbacks.

Uploaded by

Ali Shaukat
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

"/Adding TextBox Dynamically"/ for (int i = 0; i < TotalNumberAdded; ++i) { AddControls(i + 1); } // Attach the event handler to the

button Button1.Click += new EventHandler(Button1_Click); } void Button1_Click(object sender, EventArgs e) { // Increase the number added and add the new label and textbox TotalNumberAdded++; AddControls(TotalNumberAdded); } private { var var var var var void AddControls(int controlNumber) newPanel = new Panel(); newLabel = new Label(); newLabels = new Label(); newTextbox = new TextBox(); newDropDownList = new DropDownList();

// textbox needs a unique id to maintain state information newTextbox.ID = "TextBox_" + controlNumber; newLabel.Text = "Degree"; newDropDownList.ID = "DropdownList_" + controlNumber; newLabels.Text = "Year"; // add the label and textbox to the panel, then add the panel to the form newPanel.Controls.Add(newLabel); newPanel.Controls.Add(newTextbox); newPanel.Controls.Add(newLabels); newPanel.Controls.Add(newDropDownList); form1.Controls.Add(newPanel); } protected int TotalNumberAdded { get { return (int)(ViewState["TotalNumberAdded"] ?? 0); } set { ViewState["TotalNumberAdded"] = value; } }

You might also like