UserControl event & method call sequence

Discussion in 'ASP .Net Building Controls' started by James Identity, Sep 6, 2005.

  1. 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
     
    James Identity, Sep 6, 2005
    #1
    1. Advertising

  2. James Identity

    Curt_C [MVP] Guest

    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
    >
    >
    >
    >
    >
    >
     
    Curt_C [MVP], Sep 6, 2005
    #2
    1. Advertising

  3. 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:...
    > 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
    >>
    >>
    >>
    >>
    >>
    >>
     
    James Identity, Sep 6, 2005
    #3
  4. James Identity

    VJ Guest

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

    >
    >
    >
     
    VJ, Oct 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. stef mientki
    Replies:
    13
    Views:
    644
    stef mientki
    Oct 20, 2007
  2. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    183
  3. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    187
    vatech1993
    Dec 11, 2004
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    155
  5. Replies:
    1
    Views:
    184
Loading...

Share This Page