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.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55618.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3020100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1193.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3020100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 5.00160
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403020

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.67322
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403020

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


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles