You can easily implement custom properties in your custom Web Forms controls. Property arguments are set within the declarative server tag element that adds a control to a page. Properties are specified using a property name/value pair syntax where the (case-insensitive) attribute name represents the property name and the value represents the property value to assign:
<TagPrefix:Classname attributename = "propertyvalue" runat=server />
For Example:
<Samples:SampleControl Message="Hello There" MessageSize="small" Iterations=2 runat=server/>
Properties are accessed using Get/Set accessor methods in the class defining the property. A call is made to the class Set method when the control is instantiated.
The following C# example sets and retrieves the Message, MessageSize, and Iterations properties. The overridden Render method will output a line of text defined by Message in the size defined by MessageSize the number of times defined by Iterations when an ASP+ page containing the above tag is viewed in a browser.
using System; using System.Web; using System.Web.UI; namespace Samples { public enum MessageSize { Small = 0, Medium = 1, Large = 2 } public class SampleControl : Control { private String _message; private MessageSize _messageSize; private int _iterations = 1; public String Message { get { return _message; } set { _message = value; } } public MessageSize MessageSize { get { return _messageSize; } set { _messageSize = value; } } public int Iterations { get { return _iterations; } set { _iterations = value; } } protected override void Render(HtmlTextWriter output) { String htmlSize; if (_messageSize == MessageSize.Small) htmlSize="H5"; else if (_messageSize == MessageSize.Medium) htmlSize="H3"; else htmlSize="H1"; for (int i=0; i<_iterations; i++) { output.Write("<" + htmlSize + ">" + _message + "</" + htmlSize + ">"); } } } }
Accessing View State Through a Private Property
The SlideShow control defines the SlideIndex property:
private int SlideIndex { get { return (int) State["Index"]; } set { State["Index"] = value; } }
The private SlideIndex property provides a convenient means of saving and retrieving view state values used internally by the control and stored in the member of the Control.State collection named Index. Since the SlideIndex property is declared as private, it cannot be accessed externally.
Child Properties
Child properties are subproperties of control properties where the subproperties are identified with a dash ("-"). For example:
<TagPrefix:Classname propertyname-subpropertyname="propertyvalue" runat=server />
For Example:
<ASP:label font-name="Arial" runat=server />
The following example shows subproperties used to encapsulate font subproperties within the font property:
using System; using System.Web; using System.Web.UI; namespace SimpleControlSamples { public class Formater { public int Size; public String Color; public Formater(int size, String color) { this.Size = size; this.Color = color; } } public class SimpleSubProperty : Control { private Formater _format = new Formater(3, "black"); private String _message = null; public String Message { get { return _message; } set { _message = value; } } public Formater Format { get { return _format; } } protected override void Render(HtmlTextWriter output) { output.Write("<font size=" + Format.Size + " color=" + Format.Color + ">"); output.Write(_message); output.Write("</font>"); } } }
The ASP+ page that uses the SimpleSubProperty control defined above sets font Color and Size properties in the <SimpleControlSamples:SimpleSubProperty> tag:
<%@ Register TagPrefix="SimpleControlSamples" Namespace="SimpleControlSamples" %> <html> <body> <form method="POST" action="HelloWorld.aspx" runat=server> <SimpleControlSamples:SimpleSubProperty Message="Hello There" Format-Color="red" Format-Size="3" runat=server/> </form> </body> </html>