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
Leave a Comment