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#
in Page_Load of the control do:
Page.LoadComplete += delegate { LoadTheControl(); };
i'd still like to hear your ideas though.
User Control properties are used to set the values of a User Control from the parent page.
User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.
User controls are custom, reusable controls, and they use the same techniques that are employed by HTML and Web server controls. They offer an easy way to partition and reuse common user interfaces across ASP.NET Web applications. They use the same Web Forms programming model on which a Web Forms page works.
User controls are containers into which you can put markup and Web server controls. You can then treat the user control as a unit and define properties and methods for it. Custom controls. A custom control is a class that you write that derives from Control or WebControl.
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;
myControl.LoadData();
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With