The code discussion that follows is not essential for performing the steps in this walkthrough and you can skip it initially. However, if you are new to control authoring, we recommend that you read it at least after completing the walkthrough.
If your control renders a user interface (UI) element or any other visible element on the client, you should derive your control from System.Web.UI.WebControls..::.WebControl (or a derived class). If your control renders an element that is not visible in the client browser, such as a hidden element or a meta element, derive your control from System.Web.UI..::.Control. The WebControl class derives from Control and adds style-related properties such as Font, ForeColor, and BackColor. In addition, a control that derives from WebControl participates in the themes features of ASP.NET without any extra work on your part.
If your control extends the functionality of an existing control, such as the Button, Label, or Image controls, you can derive from that control. Because WelcomeLabel extends the functionality of the Label control, it could derive from Label. However, this walkthrough derives WelcomeLabel from WebControl to show how to define a property and define property metadata.
The WelcomeLabel defines one property, Text, and uses view state to store the property value. Using view state persists the value of Text across postbacks. On each postback, the page is recreated and values are restored from view state. If the Text value were not stored in view state, the value would be set to its default, Empty, on each postback. The ViewState property inherited from WebControl is a dictionary that saves data values. Values are entered and retrieved using a String key. In this case, "Text" is used as the key. Items in the dictionary are typed as Object, which you must then cast to the property type. For more information, see ASP.NET State Management Overview.
The WelcomeLabel control renders its Text property by overriding the inherited RenderContents method. The parameter passed into the RenderContents method is an object of type HtmlTextWriter, which is a utility class that has methods for rendering tags and other HTML (and HTML-variant) markup.
Notice that WelcomeLabel makes successive calls to the HtmlTextWriter object's Write method instead of performing string concatenation and then invoking the Write method. This improves performance because the HtmlTextWriter object writes directly to the output stream. String concatenation requires time and memory to create the string, and then writes to the stream. When you implement rendering in your controls, you should follow the pattern illustrated in this walkthrough.
The attributes applied to WelcomeLabel contain metadata that is used by the common language runtime and by design-time tools.
At the class level, WelcomeLabel is marked with the following attributes:
AspNetHostingPermissionAttribute is a code-access security attribute. It causes the JIT compiler to check that code that links to WelcomeLabel is granted the AspNetHostingPermission permission. All public ASP.NET classes are marked with this attribute. You should apply AspNetHostingPermissionAttribute to your controls as a security check against partially trusted callers.
DefaultPropertyAttribute is a design-time attribute that specifies the default property of a control. In visual designers, the property browser typically highlights the default property when a page developer clicks the control on the design surface.
ToolboxDataAttribute specifies the format string for the element. The string becomes the control's markup when the control is double-clicked in the toolbox or dragged from the toolbox onto the design surface. For WelcomeLabel, the string creates this element:
|
<aspSample:WelcomeLabel runat="server"> </aspSample:WelcomeLabel>
|
The WelcomeLabel control also inherits two attributes from the WebControl base class, ParseChildrenAttribute and PersistChildrenAttribute. They are applied as ParseChildren(true) and PersistChildren(false). These two attributes work together and with the ToolboxDataAttribute attribute so that child elements are interpreted as properties and properties are persisted as attributes.
The following attributes applied to the Text property of WelcomeLabel are standard design-time attributes that you will generally apply to all public properties of your controls:
BindableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to bind the property to data. For example, in Visual Studio 2005, if a property is marked with Bindable(true), the property is displayed in the DataBindings dialog box. If a property is not marked with this attribute, the property browser infers the value to be Bindable(false).
CategoryAttribute specifies how to categorize the property in the visual designer's property browser. For example, Category("Appearance") tells the property browser to display the property in the Appearance category when the page developer uses the category view of the property browser. You can specify a string argument corresponding to an existing category in the property browser or create your own category.
DescriptionAttribute specifies a brief description of the property. In Visual Studio 2005, the property browser displays the description of the selected property at the bottom of the Properties window.
DefaultValueAttribute specifies a default value for the property. This value should be the same as the default value you return from the property accessor (getter). In Visual Studio 2005, the DefaultValueAttribute allows a page developer to reset a property value to its default by bringing up the shortcut menu in the Properties window and clicking the Reset button.
LocalizableAttribute, specified as true or false, specifies for visual designers whether it is meaningful to localize the property. When a property is marked Localizable(true), the visual designer includes it when serializing localized resources. The designer will persist the property value to the culture-neutral resource file or other localization source when the control is polled for localizable properties.
Note: |
|---|
You could not apply
LocalizableAttribute to custom server controls in ASP.NET versions 1.0 and 1.1 because the ASP.NET localization model was different in those versions.
|
Design-time attributes applied to a control and its members do not affect the functioning of the control at run time, but enhance the developer experience when the control is used in a visual designer. You can see a complete listing of design-time, parse-time, and run-time attributes for server controls in Metadata Attributes for Custom Server Controls.