I came across the requirement for a project that I was working on, my manager wanted me to accomplish a specific task by sending a message to a particular member of the group. He suggested I should design the screen, where the user can pick a members name from a DropDownList and type a message in a Textbox and submit that information to the system, but at the same time he wanted me to design the system that user would be able to send a multiple request to different members of the group. In this project I have design the screen where user types a message in Textbox and select the appropriate party name from the DropDownList and send that data to database for later process. The question arose how would I retain the ViewState after the PostBack and at the same time recreate the DropDownList control data and retain the TextBoxes control property text value for a multiple request?
Before I begin read my last article Dynamically create Textbox controls from which you will learn how to create TextBoxes control dynamically on ASP.NET page, and also read about how ASP.NET page life cycle works?
In this project I decided to manually assign a control id to the TextBoxes and DropDownList. After creating these controls I just added them to different placeholder controls.
// Add TextBoxes Control to Placeholder
private void CreateTextBoxes()
{
for (int counter = 0; counter <= NumberOfControls; counter++)
{
TextBox tb = new TextBox();
tb.Width = 250;
tb.Height = 60;
tb.TextMode = TextBoxMode.MultiLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
tb.Text = "This is textbox " + counter + " data";
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br>"));
}
}
In CreateTextBoxes method I loop through ‘n’ numbers of controls that we wants to create dynamically in phTextBoxes placeholder.
// Add DropDownList Control to Placeholder
private void CreateDropDownBoxes(DataTable dtTable)
{
for (int counter = 0; counter <= NumberOfControls; counter++)
{
DropDownList ddl = new DropDownList();
ddl.ID = "DropDownListID" + (counter + 1).ToString();
int nTotalRecords = dtTable.Rows.Count;
for (int i = 0; i < nTotalRecords; i++)
{
ddl.Items.Add(dtTable.Rows[i]["item"].ToString());
}
phDropDownLists.Controls.Add(ddl);
phDropDownLists.Controls.Add(new LiteralControl("<br><br><br>"));
}
}
In CreateDropDownBoxes method I have done the same thing by looping through ‘n’ numbers of controls, the value of ‘n’ which was stored in ViewState, and create them in a phDropDownLists placeholder control.
// Create TextBoxes and DropDownList data here on PostBack.
protected override void CreateChildControls()
{
// Here we are recreating controls to persist the ViewState on every post back
if(Page.IsPostBack)
{
CreateTextBoxes();
InitializeDropDownBoxes();
}
// Create these conrols when asp.net page is created
else
{
CreateTextBoxes();
InitializeDropDownBoxes();
// Increase the control value to 1
this.NumberOfControls = 1;
}
}
CreateChildControls method, here we are recreating control on every PostBack. If the page is created the first time we just create these controls and save 1 in the ViewState so we know that we have created these controls and assigned the controls id to 1.
// Increase the counter when button is clicked and add to view state
private void btnAnotherRequest_Click(object sender, System.EventArgs e)
{
NumberOfControls += 1;
}
In button event handler we just increase the counter by 1, and save its value to ViewState for later retrieval.
Once we have created these controls on ASP.NET page, retrieving data from these dynamically created controls is easy by using FindControl method.
// Read DropDownList Data
private void ReadDropDownLists()
{
int n = this.NumberOfControls;
for (int i = 0; i<n; i++)
{
string DropDownListName = "DropDownListID" + (i+1).ToString();
DropDownList ddl = phDropDownLists.FindControl(DropDownListName) as DropDownList;
lTextData.Text += ddl.SelectedValue + "<br>";
}
}
And
// Read TextBoxes Data
private void ReadTextBoxes()
{
int n = this.NumberOfControls;
for (int i = 0; i<n; i++)
{
string boxName = "TextBoxID" + (i+1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
lTextData.Text += tb.Text + ("<br>");
}
}
Conclusion:
To persists the ViewState of child controls we recreated these controls again on a PostBack in overrideable CreateChildControls() Method which is called whenever ASP.NET needs to create child controls in a Control Tree.
Page output:
Download:
C#, ASP.NET example is included in zip format.
Demo:
Click here to see this example in action.