My most recent MSDN article - An Extensive Examination of User Controls - is now online at the ASP.NET Dev Center. This article takes a look at common User Control tasks, from creating and deploying User Controls, to creating User Controls with public properties, methods, and events. It looks at handling postbacks in User Controls, raising events from a User Control, and dynamically loading User Controls in an ASP.NET Web page.
There are a number of real-world scenarios where the ASP.NET page that contains the User Control must be alerted when some action happens in the User Control. For example, in a project I worked on a few months back there was a menu displayed in a User Control. All ASP.NET Web pages on the site displayed this menu, but some pages needed to know when the user opted to jump to another page. (Some pages would not let the user leave the page through the menu system until they had completed their task on said page.) In another project I consulted on, the developers had two User Controls on a page, each with DropDownLists. When a new selection was made in User Control 1, they wanted to update User Control 2's selections based on the DropDownList in User Control 1.
So, how does one accomplish such tasks with User Controls? There are a couple of ways. One method, as written about by Tim Stall, is to give the User Control a delegate and let the containing ASP.NET Web page assign one of its methods to the User Control's delegate. Then, the User Control can invoke the containing page's method. This could be used in the following manner: the User Control that contains the menu system has a delegate property that can be assigned a method from the containing page's code-behind class. When the user clicks on a LinkButton to go to another page, the LinkButton's Click event handler invokes the delegate. The containing ASP.NET page, then, can take whatever action is necessary: either displaying a warning to the user informing them that they cannot yet leave the page, or using a Response.Redirect() to whisk the user to their desired page. For more information on this technique read Tim's article Trigger Page Methods from a User Control.
For the scenario just described, I prefer the technique of giving the User Control an event. This event, then, is raised when the LinkButton is clicked, and the page can create an event handler for the User Control's event. This technique I discuss in An Extensive Examination of User Controls, and have used in many User Controls that I've created when the containing page might be interested in something that occurs within the User Control. What's especially nice about this technique is that it closely mimics the approach with working with the built-in server controls. Drag and drop onto the designer, and then create event handlers.
I'll leave you with this interesting factoid I learned about User Controls from a student in one of my ASP.NET classes (Bob Mohler). As you may or may not know, User Controls cannot be added to the Toolbox in Visual Studio .NET. Therefore, I reasoned, there is no drag and drop support for User Controls, so I've always manually added the needed <%@ Register %> directive by hand in the HTML portion, as well as the declarative syntax. What VS.NET does allow, though, is the ability to drag and drop a User Control from the Solution Explorer. It will then automatically add both the <%@ Register %> directive and declarative syntax. Cool, eh? Of course, if you need to programmatically access the User Control in the code-behind class, you'll still need to manually add the member variable, but at least this little tip cuts down on some of the steps. :-)