Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 218
roman m Avatar asked Sep 24 '09 21:09

roman m


People also ask

Which property is used to set the user control on the form?

User Control properties are used to set the values of a User Control from the parent page.

What is UserControl in wpf?

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.

What is user control and custom control in asp net with example?

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.

What are user controls in asp net?

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.


3 Answers

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

like image 118
rick schott Avatar answered Sep 21 '22 06:09

rick schott


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();
like image 29
David Avatar answered Sep 19 '22 06:09

David


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

like image 44
womp Avatar answered Sep 18 '22 06:09

womp