Page / UserControl OnInit Problem

Discussion in 'ASP .Net Web Controls' started by gregor.cieslak@gmail.com, Jun 9, 2006.

  1. Guest

    Hi,

    how you people handle that issue:

    you have one page and one control.
    in your page, you load some kind of object based on the given
    parameter:

    Page:

    protected override void OnInit(EventArgs e)
    {

    InitializeComponent();

    // Get Profile
    this.profile = (UserProfile)EntityCache.Get ( Request["profileId"] );

    // Set profile object to child control
    this.profileControl.Profile = this.profile


    }

    ------------------------

    ...in your control, you need that profile object the page already got

    Control:

    private UserProfile profile;

    public UserProfile Profile
    {
    get { return this.profile; }
    set { this.profile = value; }
    }


    protected override void OnInit(EventArgs e)
    {
    // Do something with the profile object
    Populate( this.Profile );
    }

    ------------------------

    now here is the problem:
    the OnInit Event in the control is always fired first,
    so i have no chance to load the profile object in the Page and then set
    it to the control, so now it leads that the profile object in the
    control is always null.

    i could solve that by moving the OnInit code in the control, to the
    Page_Load, but i need that code in the OnInit Event due custom
    events...

    how you guys handle that?

    thanks,
    gregor
     
    , Jun 9, 2006
    #1
    1. Advertising

  2. How about having the control get the profile from its parent Page, rather
    than the other way around? You could do it in the Load event.
    Peter

    --
    Co-founder, Eggheadcafe.com developer portal:
    http://www.eggheadcafe.com
    UnBlog:
    http://petesbloggerama.blogspot.com




    "" wrote:

    > Hi,
    >
    > how you people handle that issue:
    >
    > you have one page and one control.
    > in your page, you load some kind of object based on the given
    > parameter:
    >
    > Page:
    >
    > protected override void OnInit(EventArgs e)
    > {
    >
    > InitializeComponent();
    >
    > // Get Profile
    > this.profile = (UserProfile)EntityCache.Get ( Request["profileId"] );
    >
    > // Set profile object to child control
    > this.profileControl.Profile = this.profile
    >
    >
    > }
    >
    > ------------------------
    >
    > ...in your control, you need that profile object the page already got
    >
    > Control:
    >
    > private UserProfile profile;
    >
    > public UserProfile Profile
    > {
    > get { return this.profile; }
    > set { this.profile = value; }
    > }
    >
    >
    > protected override void OnInit(EventArgs e)
    > {
    > // Do something with the profile object
    > Populate( this.Profile );
    > }
    >
    > ------------------------
    >
    > now here is the problem:
    > the OnInit Event in the control is always fired first,
    > so i have no chance to load the profile object in the Page and then set
    > it to the control, so now it leads that the profile object in the
    > control is always null.
    >
    > i could solve that by moving the OnInit code in the control, to the
    > Page_Load, but i need that code in the OnInit Event due custom
    > events...
    >
    > how you guys handle that?
    >
    > thanks,
    > gregor
    >
    >
     
    Peter Bromberg [C# MVP], Jun 9, 2006
    #2
    1. Advertising

  3. Guest

    well, that would be an solution too,
    but if i have one page and multiple controls, i'd like to prevent code
    duplication.

    i'd like also let the control be independant, so i could use it at
    other places, where the parameter or whatever would be different.
    gregor.


    Peter schrieb:

    > How about having the control get the profile from its parent Page, rather
    > than the other way around? You could do it in the Load event.
    > Peter
    >
    > --
    > Co-founder, Eggheadcafe.com developer portal:
    > http://www.eggheadcafe.com
    > UnBlog:
    > http://petesbloggerama.blogspot.com
    >
    >
    >
    >
    > "" wrote:
    >
    > > Hi,
    > >
    > > how you people handle that issue:
    > >
    > > you have one page and one control.
    > > in your page, you load some kind of object based on the given
    > > parameter:
    > >
    > > Page:
    > >
    > > protected override void OnInit(EventArgs e)
    > > {
    > >
    > > InitializeComponent();
    > >
    > > // Get Profile
    > > this.profile = (UserProfile)EntityCache.Get ( Request["profileId"] );
    > >
    > > // Set profile object to child control
    > > this.profileControl.Profile = this.profile
    > >
    > >
    > > }
    > >
    > > ------------------------
    > >
    > > ...in your control, you need that profile object the page already got
    > >
    > > Control:
    > >
    > > private UserProfile profile;
    > >
    > > public UserProfile Profile
    > > {
    > > get { return this.profile; }
    > > set { this.profile = value; }
    > > }
    > >
    > >
    > > protected override void OnInit(EventArgs e)
    > > {
    > > // Do something with the profile object
    > > Populate( this.Profile );
    > > }
    > >
    > > ------------------------
    > >
    > > now here is the problem:
    > > the OnInit Event in the control is always fired first,
    > > so i have no chance to load the profile object in the Page and then set
    > > it to the control, so now it leads that the profile object in the
    > > control is always null.
    > >
    > > i could solve that by moving the OnInit code in the control, to the
    > > Page_Load, but i need that code in the OnInit Event due custom
    > > events...
    > >
    > > how you guys handle that?
    > >
    > > thanks,
    > > gregor
    > >
    > >
     
    , Jun 9, 2006
    #3
  4. Guest

    so there is no clean solution for that?
    that's pretty weak...




    schrieb:

    > well, that would be an solution too,
    > but if i have one page and multiple controls, i'd like to prevent code
    > duplication.
    >
    > i'd like also let the control be independant, so i could use it at
    > other places, where the parameter or whatever would be different.
    > gregor.
    >
    >
    > Peter schrieb:
    >
    > > How about having the control get the profile from its parent Page, rather
    > > than the other way around? You could do it in the Load event.
    > > Peter
    > >
    > > --
    > > Co-founder, Eggheadcafe.com developer portal:
    > > http://www.eggheadcafe.com
    > > UnBlog:
    > > http://petesbloggerama.blogspot.com
    > >
    > >
    > >
    > >
    > > "" wrote:
    > >
    > > > Hi,
    > > >
    > > > how you people handle that issue:
    > > >
    > > > you have one page and one control.
    > > > in your page, you load some kind of object based on the given
    > > > parameter:
    > > >
    > > > Page:
    > > >
    > > > protected override void OnInit(EventArgs e)
    > > > {
    > > >
    > > > InitializeComponent();
    > > >
    > > > // Get Profile
    > > > this.profile = (UserProfile)EntityCache.Get ( Request["profileId"] );
    > > >
    > > > // Set profile object to child control
    > > > this.profileControl.Profile = this.profile
    > > >
    > > >
    > > > }
    > > >
    > > > ------------------------
    > > >
    > > > ...in your control, you need that profile object the page already got
    > > >
    > > > Control:
    > > >
    > > > private UserProfile profile;
    > > >
    > > > public UserProfile Profile
    > > > {
    > > > get { return this.profile; }
    > > > set { this.profile = value; }
    > > > }
    > > >
    > > >
    > > > protected override void OnInit(EventArgs e)
    > > > {
    > > > // Do something with the profile object
    > > > Populate( this.Profile );
    > > > }
    > > >
    > > > ------------------------
    > > >
    > > > now here is the problem:
    > > > the OnInit Event in the control is always fired first,
    > > > so i have no chance to load the profile object in the Page and then set
    > > > it to the control, so now it leads that the profile object in the
    > > > control is always null.
    > > >
    > > > i could solve that by moving the OnInit code in the control, to the
    > > > Page_Load, but i need that code in the OnInit Event due custom
    > > > events...
    > > >
    > > > how you guys handle that?
    > > >
    > > > thanks,
    > > > gregor
    > > >
    > > >
     
    , Jun 12, 2006
    #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. Guest

    UserControl OnInit not firing

    Guest, Nov 16, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,482
    Jacob Yang [MSFT]
    Nov 17, 2003
  2. Sally
    Replies:
    1
    Views:
    480
    bruce barker
    Apr 15, 2004
  3. Raymond Du
    Replies:
    3
    Views:
    5,954
    Raymond Du
    Apr 29, 2004
  4. sourabh
    Replies:
    1
    Views:
    531
    avnrao
    May 13, 2004
  5. Replies:
    3
    Views:
    585
Loading...

Share This Page