Page / UserControl OnInit Problem

G

gregor.cieslak

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
 
G

Guest

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
 
G

gregor.cieslak

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.

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




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
 
G

gregor.cieslak

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




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.

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




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
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top