B
Bas Groeneveld
I am developing an ASP.NET application part of which consists of a data
entry wizard defined by entries in a data table - ie the controls on each
page of the wizard are determined by definitions in the table.
I know that I can dynamically add controls (eg a textbox) to the page
controls collection of a web form in a server event which will then be
rendered onto the form, as in the following snippet:
System.Web.UI.WebControls.TextBox tbTest = new
System.Web.UI.WebControls.TextBox();
tbTest.ID = "tbTest";
tbTest.Text = "Test Textbox";
tbTest.EnableViewState = true;
tbTest.Style["Position"] = "Absolute";
tbTest.Style["Top"] = "35px";
tbTest.Style["Left"] = "150px";
this.Controls.Add(tbTest);
The problem I have is on the subsequent postback. The control dynamically
added to the page controls collection is no longer accessible when the page
is posted back. ie the following code does not work:
if (this.FindControl("tbTest") != null)
lblMessage.Text = ((TextBox)(this.FindControl("tbTest"))).Text;
The only way I can get it to work is to create the control again on postback
in the page_load event:
if (IsPostBack)
{
System.Web.UI.WebControls.TextBox tbTest = new
System.Web.UI.WebControls.TextBox();
tbTest.ID = "tbTest";
tbTest.Visible = false;
this.Controls.Add(tbTest);
}
If I do this the control and any text entered can be accessed.
However this means that my page_load event has to know to add the same
dynamic controls as were added dynamically in another event (eg Next button
click) and then I need to delete these controls before I dynamically create
the controls needed for the next step of the wizard.
Is this the only way to handle this, or am I missing something obvious?
Thanks
Bas
--
==========================================
Bas Groeneveld
Benchmark Design System and Software Engineering
PO Box 165N, Ballarat North, VIC 3350
Phone: +61 3 5333 5441 Mob: 0409 954 501
entry wizard defined by entries in a data table - ie the controls on each
page of the wizard are determined by definitions in the table.
I know that I can dynamically add controls (eg a textbox) to the page
controls collection of a web form in a server event which will then be
rendered onto the form, as in the following snippet:
System.Web.UI.WebControls.TextBox tbTest = new
System.Web.UI.WebControls.TextBox();
tbTest.ID = "tbTest";
tbTest.Text = "Test Textbox";
tbTest.EnableViewState = true;
tbTest.Style["Position"] = "Absolute";
tbTest.Style["Top"] = "35px";
tbTest.Style["Left"] = "150px";
this.Controls.Add(tbTest);
The problem I have is on the subsequent postback. The control dynamically
added to the page controls collection is no longer accessible when the page
is posted back. ie the following code does not work:
if (this.FindControl("tbTest") != null)
lblMessage.Text = ((TextBox)(this.FindControl("tbTest"))).Text;
The only way I can get it to work is to create the control again on postback
in the page_load event:
if (IsPostBack)
{
System.Web.UI.WebControls.TextBox tbTest = new
System.Web.UI.WebControls.TextBox();
tbTest.ID = "tbTest";
tbTest.Visible = false;
this.Controls.Add(tbTest);
}
If I do this the control and any text entered can be accessed.
However this means that my page_load event has to know to add the same
dynamic controls as were added dynamically in another event (eg Next button
click) and then I need to delete these controls before I dynamically create
the controls needed for the next step of the wizard.
Is this the only way to handle this, or am I missing something obvious?
Thanks
Bas
--
==========================================
Bas Groeneveld
Benchmark Design System and Software Engineering
PO Box 165N, Ballarat North, VIC 3350
Phone: +61 3 5333 5441 Mob: 0409 954 501