Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > UserControl event & method call sequence

Reply
Thread Tools

UserControl event & method call sequence

 
 
James Identity
Guest
Posts: n/a
 
      09-06-2005
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





 
Reply With Quote
 
 
 
 
Curt_C [MVP]
Guest
Posts: n/a
 
      09-06-2005
Have you tried wrapping the IsPostBack() check to ensure you aren't rebinding
when un-needed?

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com



"James Identity" wrote:

> 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
>
>
>
>
>
>

 
Reply With Quote
 
 
 
 
James Identity
Guest
Posts: n/a
 
      09-06-2005
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();
>> 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

myControl.BindPage();

When the page is posted back with the user control it changes seqence.

>> 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

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.


"Curt_C [MVP]" <software_at_darkfalz.com> wrote in message
news:(E-Mail Removed)...
> Have you tried wrapping the IsPostBack() check to ensure you aren't
> rebinding
> when un-needed?
>
> --
> Curt Christianson
> site: http://www.darkfalz.com
> blog: http://blog.darkfalz.com
>
>
>
> "James Identity" wrote:
>
>> 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
>>
>>
>>
>>
>>
>>



 
Reply With Quote
 
VJ
Guest
Posts: n/a
 
      10-25-2005
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.


"James Identity" wrote:

> 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();
> >> 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

> myControl.BindPage();
>
> When the page is posted back with the user control it changes seqence.
>
> >> 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

> 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.
>
>
> "Curt_C [MVP]" <software_at_darkfalz.com> wrote in message
> news:(E-Mail Removed)...
> > Have you tried wrapping the IsPostBack() check to ensure you aren't
> > rebinding
> > when un-needed?
> >
> > --
> > Curt Christianson
> > site: http://www.darkfalz.com
> > blog: http://blog.darkfalz.com
> >
> >
> >
> > "James Identity" wrote:
> >
> >> 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
> >>
> >>
> >>
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call a Method in a UserControl from another UserControl jsausten@hotmail.com ASP .Net Web Controls 1 08-11-2006 09:53 AM
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 1 12-11-2004 01:28 PM
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 0 12-10-2004 02:21 PM
Event not firing in usercontrol inside usercontrol vatech1993@yahoo.com ASP .Net Building Controls 0 12-10-2004 02:20 PM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments