Dynamically adding web controls to a page

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
 
B

Bas Groeneveld

Thanks Martin,

I had seen the KB article but hadn't seen the User Interface Application
Process Block stuff. What it, and other articles on Dynamic Controls, gloss
over is the requirement to create the dynamic controls each time around.

I had come to the conclusion that all dynamic controls had to be created
every time, through trial and error, and it makes sense when you consider
the stateless nature of the beast. I was hoping that there was another way
as it means that the 'previous' controls have to be recreated to allow the
entered data to be accessed, presumably these will then have to be removed
before the 'next' set of controls are added to the page.

I also found that while I can initially create the controls in a button
click handler, the only way to access the control on postback is to create
them in the Page_Load or InitializeComponent functions. Presumably this is
so that the data from the ViewState can be applied to them.

Sounds like a good job for a state machine, which is presumably what the
User Interface Application Process Block stuff implements.

Bas

Martin Marinov said:
You have to recreate controls every time you visit the page ( with or
without postback). Take a look at this article
http://support.microsoft.com/default.aspx?scid=kb;EN-US;317794

also for creating wizard you can check the User Interface Application
Process Block here :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/uip.asp

Regards
Martin

Bas Groeneveld said:
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
 
G

Guest

The control SHOULD be save in the ViewState. Check to see if it is. If not, you may have to save it there manually.
 
B

Bas Groeneveld

I agree, and it does appear to be. However the only way I can get it out is
to create the control again on postback and then to reference its text
property.

Is there another way? How are controls saved in the viewstate? Something
like lblMessage.Text = ViewState["<key>"].ToString() should work if I know
the <key>.

Bas

The control SHOULD be save in the ViewState. Check to see if it is. If
not, you may have to save it there manually.
Bas Groeneveld said:
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
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top