Iver's web place

Life is a journey ... taken one shot at a time!

How to create custom controls in ASP.NET part 1 of 3

This is my first post in English, I need more practice so ... If you find some error in my redaction, feel free to comment about it.
For understand the topic is necesary to know the basic differences between user controls and custom controls:
User Control
  • Designed for single-application scenarios
  • Deployed in the source form (.ascx) along with the source code of the application
  • If the same control needs to be used in more than one application, it introduces redundancy and maintenance problems
Custom control
  • Designed so that it can be used by more than one application
  • Deployed either in the application's Bin directory or in the global assembly cache
  • Distributed easily and without problems associated with redundancy and maintenance
OK, After that I want to show you how we can create a custom control in ASP.NET for this I split the post in 3 parts:
  1. Design-time Attributes
  2. State Management
  3. Deploying Custom Controls
Update: Becouse the topic was less extensive that I thought, I wrote 3 articles only

Design-time Attributes

There are three ways for add attributes to our control:
a) Property-Level Attributes
b) Class-Level Attributes
c) Assembly-Level Attributes
a) Property-Level Attributes
You can annotate properties with the BrowsableAttribute, DescriptionAttribute, DefaultValueAttribute and CategoryAttribute design-time attributes. For example, in the following code:
[BrowsableAttribute(true)]
[DescriptionAttribute("Gets and sets the product id selected")]
[DefaultValueAttribute(0)]
[CategoryAttribute("Appearence")]
public virtual int ProductId{
   get { return this.productID; }
   set { this.productID = value; }
}
 
This code to instruct the property browser:
  • To display the name and value of this property. By default, every public property is considered browsable.
  • To display the text "Gets and sets the product id selected" every time the page developer select the property.
  • To display this property under the appearance category.
b) Class-Level Attributes
The following code annotates the MyCustomControl custom control with the class-level attribute ToolboxData attribute to specify default values for properties of the MyCustomControl control:
[ToolboxData("<{0}:MyCustomControl Name='CustomName' Text='Personalized Text' runat='server' ></{0}:MyCustomControl>")]
public class MyCustomControl : Control
 
This attribute instructs the designer to add the following line to the .aspx page when the page developer drags MyCustomControl from the Toolbox into the designer surface:
<cc1:MyCustomControl Name='CustomName' Text='Personalized Text' runat='server' >
</cc1:MyCustomControl>
 
c) Assembly-Level Attributes
Before we can use the custom control, we must add the Register directive in the web page:
<%@ Register TagPrefix="cc1" Namespace="CustomComponents" %>
...
<cc1:MyCustomControl Name="CustomName" Text="Personalized Text" runat="server" />
 
The designer use the default prefix cc1. You can add the following assembly-level attribute to the AssemblyInfo.cs file to instruct the designer to use "myTag" as the tag prefix:
using System.Web.UI;
[assembly: TagPrefix("CustomComponents","myTag")]
 
With this, the designer use the myTag prefix instead of cc1.
<%@ Register Assembly="CustomComponets" TagPrefix="myTag" Namespace="CustomComponents" %>
...
<myTag:MyCustomControl Name="CustomName" Text="Personalized Text" runat="server" />
 
Resources

http://support.microsoft.com/kb/893667

http://msdn.microsoft.com/en-us/library/aa288059(VS.71).aspx

Professional ASP.NET 2.0 Server Control and Component Development
Dr. Shahram Khosravi
ISBN-13: 978-0-471-79350-2
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471793507.html
Trackback URI: http://en.iver.com.mx/index.php?trackback/1

#1 Re: How to create custom controls in ASP.NET part 1 of 3

rapid4me, <fiyo(at)gmaildotcom> / 25 November 2009  
avatar

Ok, thanks a lot for your post. It was of good help to me, hope to hear from you soon again.

[ Reply (0) ]

Leave a Comment

Write the captcha code you are seeing.

Comment XML feeds: RSS | Atom

Blog Calendar

July 2010
Sun Mon Tue Wed Thu Fri Sat
27 28 29 30 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7