471,078 Members | 785 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

force dynamically added controls to be initialized

Hi,

I have a complex asp.net 1.1 program that dynamically needs to load controls
(LoadControl)
The textboxes etc inside these controls keep viewstate and values but not
within
Page_Load()

when that event is 'going on' the load control happens but the values are
not yet 'filled in' They are filled in only -when- the pageload event has
been executed completely.

QUESTION: How to fix this behaviour?

So

Private MyControl myControl;

void PageLoad(object sender, EventArgs e)
{
this.myControl = (myControl) LoadControl("myControl.ascx");

myPlaceHolder.Controls.Add(myControl);

if (IsPostBack)
string temp = this.myControl.someProperty; // maps to internal
TextBox!

ïf (temp.Length == 0)
lblError.Text = "empty!";
}

}

Nov 18 '05 #1
3 2102
Hi Egbert,

You're an IIS guy, so it should be obvious to you that each Request for an
ASPX page happens in a vacuum, as HTTP is stateless. What this means with
regards to your issue is that, when you dynamically load a Control class
into your Page, regardless of whether the Page is posted back or not, you
are loading a brand-new instance of the Control to a brand-new instance of
the Page.

The LoadViewState event occurs quite early on in the Control Execution
LifeCycle, right after the Init event. So, the solution to your dilemma is
to load your User Controls in the Init event of the Page.

The following .Net SDK article details the Events that occur in the
LifeCycle of every System.Web.UI.Control (including the Page class):

http://msdn.microsoft.com/library/de...nLifecycle.asp

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:#J**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a complex asp.net 1.1 program that dynamically needs to load controls (LoadControl)
The textboxes etc inside these controls keep viewstate and values but not
within
Page_Load()

when that event is 'going on' the load control happens but the values are
not yet 'filled in' They are filled in only -when- the pageload event has
been executed completely.

QUESTION: How to fix this behaviour?

So

Private MyControl myControl;

void PageLoad(object sender, EventArgs e)
{
this.myControl = (myControl) LoadControl("myControl.ascx");

myPlaceHolder.Controls.Add(myControl);

if (IsPostBack)
string temp = this.myControl.someProperty; // maps to internal
TextBox!

f (temp.Length == 0)
lblError.Text = "empty!";
}

}

Nov 18 '05 #2
textboxes do not use viewstate for their value, they get from postback data
event.

-- bruce (sqlwork.com)
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
Hi Egbert,

You're an IIS guy, so it should be obvious to you that each Request for an
ASPX page happens in a vacuum, as HTTP is stateless. What this means with
regards to your issue is that, when you dynamically load a Control class
into your Page, regardless of whether the Page is posted back or not, you
are loading a brand-new instance of the Control to a brand-new instance of
the Page.

The LoadViewState event occurs quite early on in the Control Execution
LifeCycle, right after the Init event. So, the solution to your dilemma is
to load your User Controls in the Init event of the Page.

The following .Net SDK article details the Events that occur in the
LifeCycle of every System.Web.UI.Control (including the Page class):

http://msdn.microsoft.com/library/de...nLifecycle.asp
--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:#J**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a complex asp.net 1.1 program that dynamically needs to load

controls
(LoadControl)
The textboxes etc inside these controls keep viewstate and values but not within
Page_Load()

when that event is 'going on' the load control happens but the values are not yet 'filled in' They are filled in only -when- the pageload event has been executed completely.

QUESTION: How to fix this behaviour?

So

Private MyControl myControl;

void PageLoad(object sender, EventArgs e)
{
this.myControl = (myControl) LoadControl("myControl.ascx");

myPlaceHolder.Controls.Add(myControl);

if (IsPostBack)
string temp = this.myControl.someProperty; // maps to internal
TextBox!

f (temp.Length == 0)
lblError.Text = "empty!";
}

}


Nov 18 '05 #3
> textboxes do not use viewstate for their value, they get from postback
data
event.
That is true. However, it would not affect the advice I gave him. The
LoadPostData Event occurs just after the LoadViewState Event, which occurs
just after the Init Event.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"bruce barker" <no***********@safeco.com> wrote in message
news:uw**************@TK2MSFTNGP10.phx.gbl... textboxes do not use viewstate for their value, they get from postback data event.

-- bruce (sqlwork.com)
"Kevin Spencer" <ks******@takempis.com> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
Hi Egbert,

You're an IIS guy, so it should be obvious to you that each Request for an
ASPX page happens in a vacuum, as HTTP is stateless. What this means with regards to your issue is that, when you dynamically load a Control class
into your Page, regardless of whether the Page is posted back or not, you are loading a brand-new instance of the Control to a brand-new instance of the Page.

The LoadViewState event occurs quite early on in the Control Execution
LifeCycle, right after the Init event. So, the solution to your dilemma is to load your User Controls in the Init event of the Page.

The following .Net SDK article details the Events that occur in the
LifeCycle of every System.Web.UI.Control (including the Page class):

http://msdn.microsoft.com/library/de...nLifecycle.asp

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
I get paid good money to
solve puzzles for a living

"Egbert Nierop (MVP for IIS)" <eg***********@nospam.invalid> wrote in
message news:#J**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a complex asp.net 1.1 program that dynamically needs to load

controls
(LoadControl)
The textboxes etc inside these controls keep viewstate and values but

not within
Page_Load()

when that event is 'going on' the load control happens but the values are not yet 'filled in' They are filled in only -when- the pageload event has been executed completely.

QUESTION: How to fix this behaviour?

So

Private MyControl myControl;

void PageLoad(object sender, EventArgs e)
{
this.myControl = (myControl) LoadControl("myControl.ascx");

myPlaceHolder.Controls.Add(myControl);

if (IsPostBack)
string temp = this.myControl.someProperty; // maps to internal
TextBox!

f (temp.Length == 0)
lblError.Text = "empty!";
}

}



Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bas Groeneveld | last post: by
4 posts views Thread by Harry | last post: by
5 posts views Thread by Chris | last post: by
1 post views Thread by jelle.huygen | last post: by
5 posts views Thread by Nathan Sokalski | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.