Viewstate and dynamic controls

G

Guest

I have a form with two dynamically created controls. One control is a list
box, the other is a text box. I would like to set the value of the text box
based on the selection of the list box. I have set AutoPostBack for the list
box to true so that I get a post on selection change.
Even though I recreate the textbox on each postback, I cannot affect the
value to the text box.

This is the code:

private void Page_Load(object sender, System.EventArgs e)
{
this.EnableViewState = false;
ListBox1 = new System.Web.UI.WebControls.ListBox();
TextBox1 = new System.Web.UI.WebControls.TextBox();
ListBox1.AutoPostBack = true;
ListBox1.ID = "ListBox1";
ListBox1.Items.Add("one");
ListBox1.Items.Add("two");
Form1.Controls.Add(ListBox1);

if (IsPostBack){
TextBox1.Text = "second";
}
else {
TextBox1.Text = "first";
}
TextBox1.ID = "TextBox1";
Form1.Controls.Add(TextBox1);
}
If I change the ID of the TextBox on the postback, of course the value will
be changed appropriately. I seem to be caught in viewstate hell. I have
tried to set EnableViewState to false, but that is ignored by .NET. How can
I override this viewstate persistence?
Thanks in advance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top