force dynamically added controls to be initialized

  • Thread starter Egbert Nierop \(MVP for IIS\)
  • Start date
E

Egbert Nierop \(MVP for IIS\)

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!";
}

}
 
K

Kevin Spencer

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/d...guide/html/cpconControlExecutionLifecycle.asp

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

bruce barker

textboxes do not use viewstate for their value, they get from postback data
event.

-- bruce (sqlwork.com)
 
K

Kevin Spencer

textboxes do not use viewstate for their value, they get from postback
data

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
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top