How to set user control properties before it gets rendered?

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.


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

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;

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().

Well, None of the answer help me. But when a set manually the properties on the event InitComplete() everything works fine. My user control was, now, enabled to read the property AFTER i set the property not before.

I have encountered a similar problem:-

I first add a UserControl dynamically, and then attempt to assign values to the control's internal controls (a textbox within the user control). Using any event such as INIT or LOAD, the control gives me a null reference exception, because as evidenced by stepping through the code, the controls have not as yet been created.

Later I found that this problem only occurs if I declare the control through a dim statement;

Dim MyControl as new MyUserControl
MyControl.SetLabelText("My Name") 'Uses a method to set the label text on load.

On the other hand, if I use the control as follows it works just fine:

Dim MyControl as MyUserControl = LoadControl("MyUserControl.ascx")
MyControl.SetLabelText("My Label Text")

Need Your Help

Is there a shortcut for adding fields to existing constructor in Eclipse?

java eclipse keyboard-shortcuts code-generation

Is there any shortcut in Eclipse that allows me to add a field to the argument list of an existing constructor?

change highcharts detetime label


I have looked at the HighCharts API and I came across dateTimeLabel but I haven't been able to set the label properly. Here is my code, for the options