Re: UserControl event & method call sequence



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:E814875A-E78A-41E0-BBB0-637628718EF1@xxxxxxxxxxxxxxxx
> > 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
> >>
> >>
> >>
> >>
> >>
> >>
>
>
>
.



Relevant Pages

  • Re: IsPostBack with Response.Redirect to same page
    ... specific user control depending on the Selected Tab (which is ... page) it loads a specified user control. ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: UserControl event & method call sequence
    ... As the aspx page loads it then loads the ascx usercontrol. ... After loading the user control the aspx page calls a method in the user ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • Re: UserControl event & method call sequence
    ... The host aspx file is very dynamic. ... It does not always use this user control ... When the user control is loaded for the first time I set the data record: ... When the page first loads it calls Load then SetDataRecordis called then ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • UserControl event & method call sequence
    ... I have an aspx page that loads an ascx user control. ... As the aspx page loads it then loads the ascx usercontrol. ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)
  • .Net Assembly as ActiveX control
    ... I have written a windows forms user control as a .net assembly and tried to ... imbed as an object in my aspx pages but it does not happen. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)

Loading