UserControl event & method call sequence

J

James Identity

I have an aspx page that loads an ascx user control.

This is what happens:
1. As the aspx page loads it then loads the ascx usercontrol.
2. After loading the user control the aspx page calls a method in the user
control

Control c1 = LoadControl("UserControl1.ascx");
PlaceHolderControl.Controls.Add(c1);
UserControl1 myControl = (UserControl1) c1;
myControl.Id = "myControl";
myControl.SetDataRecord(myRecord);//Record that contains data needed by the
control.

When the control first loads it is ok because the Load method of the user
control is
called and then the SetDataRecord(MyRecord myRecord) is called.

When the page is posted back there is a problem.
The SetDataRecord is called first and then the Load method of the user
control is called.
This is causing a problem.

I need the control to load in a consistent way so that I can program it.
I need the control to load first every time and then I need to set the data
record and then there are more functions in the PreRender event.

On postback, the difference is that there is ViewState data.
How does the method get called on the control after it loads
and how does the method get called before it loads. How do I make this
consistant.

Also.
What determines when the user control life cycle evens are called and how
does that synchronize to the life cycle events to the aspx page that loads
the control ?

Thanks for any advice.
James
 
C

Curt_C [MVP]

Have you tried wrapping the IsPostBack() check to ensure you aren't rebinding
when un-needed?
 
J

James Identity

The host aspx file is very dynamic. It does not always use this user control
after
the page has been posted back. If the aspx file is posted back then the user
control
will show as being posted back even if it is just loaded for the first time.

When the user control is loaded for the first time I set the data record:
SetDataRecord(myRecord);//
Then I call a method to bind the page from the record.

When the page is posted back I set the data record:
SetDataRecord(myRecord);//
Then I call a function to bind the record from the page. So I get any
changes to the
user control form to update the record.

Then I remove the user control from the aspx file and load it again and set
a new record
to bind the data.

When the page first loads it calls Load then SetDataRecord() is called then
BindPage();myControl.BindPage();

When the page is posted back with the user control it changes seqence.
myControl.BindRecordFromPage();

It is called like this:
<p>SetDataRecord Called</p>
<p>BindRecordFromPage Called</p>
<p>User Control Load Method</p>

You can see how this causes a problem.
 
V

VJ

You might also find this helpful:

It is very important to understand the ASP.NET page life cycle. If you have
a usercontrol on an aspx page, following are the order of events.

1) User control Page_Init fires first
2) Page's Page_Init fires
3) Page's Page_Load fires
4) User control's Page_Load fires
5) Page's Prerender event
6) User control's Prerender event

You can test this by putting breakpoints for each events.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top