Hi flyin,
User controls are very capable in displaying dynamic data. In fact, User Controls are little ASP.NET pages, so you can anything with a UC that you can also do with a page.
There are a couple of methods to pass data to a UC. For example, you can use form fields, query strings, cookies etc for the dynamic data, like you would in any other page.
However, another, more common scenario is to have a UC expose public properties that you can set in the page that loads the UC. For example, you can have a menu that displays main and sub categories on a site. Whenever a specific article is loaded, the article itself knows to what main and subcategory it belongs. So, the article page that loads the menu UC can change public properties on the UC to preselect some items in the menu.
To be able to set the properties, you can load a control in a page using LoadControl, cast it to a specific type and then set the properties.
Here's a quick example. The example shows a public property on a UC, and a way to load the UC and set the property from a page.
Code:
[Code Behind of UC]
public int MenuItem
{
get
{
return menuItem;
}
set
{
menuItem = value;
}
}
The ASP.NET page that uses this UC can now set its properties, like this:
Code:
[Code Behind of page using UC]
MyUserControl MyUserControl1 = (MyUserControl) LoadControl("/Controls/MyUserControl.ascx");
MyUserControl1.MenuItem = 35;
MyPlaceHolder.Controls.Add(MyUserControl1);
The LoadControl method returns a generic Control object, so you need to cast it to your control type to be able to set its property MenuItem. Finally, the control is added to the Controls collection of another control, a PlaceHolder in this example.
Inside the control, you can now use menuItem to change appearance, state, load data etc etc.
So, if you look at this, you can see that UCs can be very dynamic. Since you can programmatically access them, they can do about anything you want. You could have a UC with a "Top 5" products with a repeater that displays the 5 most popular products based on a specific category. Or you could have a UC that displays different information depending on the role or access level of the currently logged on user. Or you can have a UC to display a shopping cart, showing data ranging from nothing, to a complete shopping list with order summary etc.
For all this, you could also write server controls, but for many tasks, the time it takes to write them properly, is way too much when compared with a quick UC. Of course server controls have other benefits like design time support that make them very useful in some scenarios....
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.