Scott on Writing

Musings on technical writing...

User Controls Article Available

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

posted on Wednesday, March 24, 2004 9:19 AM

Feedback

# re: User Controls Article Available 3/24/2004 9:34 AM AndrewSeven

I really didn't like the direct use of a delegate in that article.

It seems conceptualy wrong, and I feel it might point people in the wrong direction.

# re: User Controls Article Available 3/24/2004 12:20 PM RP

Figures seem to be missing from your article...

# re: User Controls Article Available 3/24/2004 12:34 PM Scott Mitchell

Thanks for the heads up, RP. I've notified my editor at MSDN.

# re: User Controls Article Available 4/1/2004 12:55 PM Aaron Robinson

Would the addition of a TagPrefixAttribute allow you to add the control to the Toolbox?

<Assembly: TagPrefix("MyNamespace", "myTag")>

# re: User Controls Article Available 12/7/2004 9:07 AM satty

Not useful at all coz Its not been explained with proper code

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<July 2008>
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Comment Stats

DayTotal% of Total
Sunday 1896.8%
Monday 39014.0%
Tuesday 46916.8%
Wednesday 51518.5%
Thursday 54419.5%
Friday 50818.2%
Saturday 1706.1%
Total 2785100.0%

Hour1Total% of Total
12:00 AM 682.4%
1:00 AM 712.5%
2:00 AM 632.3%
3:00 AM 752.7%
4:00 AM 572.0%
5:00 AM 1093.9%
6:00 AM 1114.0%
7:00 AM 1615.8%
8:00 AM 1756.3%
9:00 AM 1505.4%
10:00 AM 1736.2%
11:00 AM 1826.5%
12:00 PM 1906.8%
1:00 PM 1766.3%
2:00 PM 1605.7%
3:00 PM 1324.7%
4:00 PM 1124.0%
5:00 PM 983.5%
6:00 PM 913.3%
7:00 PM 993.6%
8:00 PM 853.1%
9:00 PM 802.9%
10:00 PM 833.0%
11:00 PM 843.0%
Total 2785100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.14144
Monday 5.35353
Tuesday 4.35444
Wednesday 7.58644
Thursday 6.87625
Friday 5.45414
Saturday 5.03161
Total 5.802785

Hour1 Entry MadeAvg.Total
12:00 AM 5.0035
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 7.0035
8:00 AM 5.45109
9:00 AM 6.34279
10:00 AM 6.41250
11:00 AM 4.28184
12:00 PM 6.98342
1:00 PM 2.87112
2:00 PM 5.29222
3:00 PM 8.54299
4:00 PM 3.9190
5:00 PM 5.78156
6:00 PM 4.52113
7:00 PM 9.32177
8:00 PM 9.06154
9:00 PM 5.14113
10:00 PM 6.2381
11:00 PM 4.5732
Total 5.802785

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles