views:

264

answers:

3

I have a User Control (uc) on a page. uc exposes some properties that get set on Page_Load() event of the parent page, and should be read while user control loads up.

Looks like Page_Load() of the uc fires before any properties get set from the parent page.

On what event should I set the uc properties so that it can use those properties as it gets rendered?

I use ASP.NET 3.5 and C#

p.s. i just dug out a solution from my old code:

in Page_Load of the control do:

Page.LoadComplete += delegate { LoadTheControl(); };

i'd still like to hear your ideas though.

+1  A: 

I JUST ran into this problem this afternoon myself.

I created a public method on the UserControl called LoadData() and called it from the Page_Load of my hosting page after setting the property that the data depended on.

Added - code example

In the user control:

public property SomeProp {get, set};

public void LoadData()
{
   // do work
}

and in the Page_Load on the hosting page

myControl.SomeProp = 1;
myControl.LoadData();
David Stratton
i guess i had the same solution (+1)
roman m
+2  A: 

You can get access to the property OnPreRender without any tricks.

rick schott
thanx, OnInit work fine too
roman m
+1  A: 

If you're putting the usercontrol on the page declaratively, just set the property there.

<cc1:myUserControl MyProperty="1" />

If you're adding the controls to the page dynamically, just set the property before you add the control to the hosting page's control collection. None of the lifecycle events for the control will fire until you call Page.Controls.Add(myUserControl).

If it is impossible to avoid the setup you've got right now, and the parent must perform some logic in Page_Load and the UC has to be there already, then I would suggest what David Stratton posted, but in my experience this is usually standardized to be called Initialize() or Init().

womp