ArrayList Resetting When Adding Form Controls
-
Below is test code to replicate the problem. I trying to add form controls to an ArrayList. In LinkButton1_Click, two textbox controls are added to ArrayList. No problem. But when i break this code into two separate LinkButtons, only one textbox will ever be added to ArrayList when both link buttons are clicked. The last textbox added will overwrite the first one added. The ArrayList.Add should not behave like this. What is going on? How should i make this work? To store they control i am using an ArrayList. public partial class Builder_Generator : System.Web.UI.Page { ArrayList _controls = new ArrayList(); TextBox _textBox = null; protected void LinkButton1_Click(object sender, EventArgs e) { _textBox = new TextBox(); _textBox.Text = "first"; _textBox.ID = "first_Textbox"; Form_PlaceHolder.Controls.Add(_textBox); _textBox = new TextBox(); _textBox.Text = "second"; _textBox.ID = "second_Textbox"; Form_PlaceHolder.Controls.Add(_textBox); } protected void LinkButton2_Click(object sender, EventArgs e) { _textBox = new TextBox(); _textBox.Text = "first"; _textBox.ID = "first_Textbox"; _controls.Add(_textBox); } protected void LinkButton3_Click(object sender, EventArgs e) { _textBox = new TextBox(); _textBox.Text = "second"; _textBox.ID = "second_Textbox"; _controls.Add(_textBox); } }