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.
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.