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
Update: Becouse the topic was less extensive that I thought, I wrote 3 articles only
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:
This code to instruct the property browser:
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:
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:
c) Assembly-Level Attributes
Before we can use the custom control, we must add the Register directive in the web page:
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:
With this, the designer use the myTag prefix instead of cc1.
Resources
Dr. Shahram Khosravi
ISBN-13: 978-0-471-79350-2
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0471793507.html
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
- 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
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; }
}
[DescriptionAttribute("Gets and sets the product id selected")]
[DefaultValueAttribute(0)]
[CategoryAttribute("Appearence")]
public virtual int ProductId{
get { return this.productID; }
set { this.productID = value; }
}
- 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.
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
public class MyCustomControl : Control
<cc1:MyCustomControl Name='CustomName' Text='Personalized Text' runat='server' >
</cc1:MyCustomControl>
</cc1:MyCustomControl>
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" />
...
<cc1:MyCustomControl Name="CustomName" Text="Personalized Text" runat="server" />
using System.Web.UI;
[assembly: TagPrefix("CustomComponents","myTag")]
[assembly: TagPrefix("CustomComponents","myTag")]
<%@ Register Assembly="CustomComponets" TagPrefix="myTag" Namespace="CustomComponents" %>
...
<myTag:MyCustomControl Name="CustomName" Text="Personalized Text" runat="server" />
...
<myTag:MyCustomControl Name="CustomName" Text="Personalized Text" runat="server" />
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 DevelopmentDr. 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
Leave a Comment
Comment XML feeds: RSS | Atom
#1 Re: How to create custom controls in ASP.NET part 1 of 3
Ok, thanks a lot for your post. It was of good help to me, hope to hear from you soon again.