By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,810 Members | 818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,810 IT Pros & Developers. It's quick & easy.

Dynamically adding web controls to a page

P: n/a
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

Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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...b;EN-US;317794

also for creating wizard you can check the User Interface Application
Process Block here :
http://msdn.microsoft.com/library/de...a/html/uip.asp

Regards
Martin

"Bas Groeneveld" <no****@nospam.com.au> wrote in message
news:31*****************@news-server.bigpond.net.au...
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

Nov 18 '05 #2

P: n/a
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" <me********@mecrossroad.bg> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
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...b;EN-US;317794

also for creating wizard you can check the User Interface Application
Process Block here :
http://msdn.microsoft.com/library/de...a/html/uip.asp
Regards
Martin

"Bas Groeneveld" <no****@nospam.com.au> wrote in message
news:31*****************@news-server.bigpond.net.au...
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


Nov 18 '05 #3

P: n/a
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" wrote:
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

Nov 18 '05 #4

P: n/a
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

<David Coe>; "MCAD" <Da**********@discussions.microsoft.com> wrote in
message news:C0**********************************@microsof t.com...
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" wrote:
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

Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.