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 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.
From Label, WelcomeLabel inherits the Text property. WelcomeLabel overrides the RenderContents method to provide the logic for creating the text to display. 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.
The WelcomeLabel uses view state to store the value for the NameForAnonymousUser property. Using view state persists the value of NameForAnonymousUser across postbacks. On each postback, the page is recreated and the value is restored from view state. If the value was 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 by using a String key. In this case, "NameForAnonymousUser" 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.
Notice that WelcomeLabel makes successive calls to the Write method of the HtmlTextWriter object, 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 just-in-time (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. For more information, see Code Access Security. For more information about the JIT compiler, see Compiling MSIL to Native Code.
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 attribute 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 so that child elements are interpreted as properties and properties are persisted as attributes.
The following attributes applied to the NameForAnonymousUser property of WelcomeLabel are standard design-time attributes that you will generally apply to 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, 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, 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 that you return from the property accessor (getter). In Visual Studio, the DefaultValueAttribute allows a page developer to reset a property value to its default by displaying 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 localized resources are being serialized. 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.
Design-time attributes applied to a control and its members do not affect the functionality of the control at run time. They 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.
A tag prefix is the prefix, such as "asp" in <asp:Table />, that appears before a control's type name when the control is created declaratively in a page. To enable your control to be used declaratively in a page, ASP.NET needs a tag prefix that is mapped to your control's namespace. A page developer can provide a tag prefix/namespace mapping by adding an @ Register directive on each page that uses the custom control, or by specifying the tag prefix and namespace in the Web.config file. Adding the tag prefix and namespace in the Web.config is useful when the custom control is used in multiple Web pages in the Web site. In this walkthrough topic, you will add the tag prefix in the Web page. For more information about how to add the tag prefix in the Web.config file, see controls Element for pages (ASP.NET Settings Schema).
Note: |
|---|
When you do not provide a value for the
assembly attribute in the @ Register directive, ASP.NET infers that the control is dynamically compiled from source files in the App_Code directory.
|
The following procedure shows how to register the custom control in an individual Web page.
To add a tag prefix mapping in a Web page
You can now use the custom control in an ASP.NET Web page.
To create a page that uses the custom control
Copy the following markup into the Default.aspx file and save the file.
<head id="Head1" runat="server">
<title>WelcomeLabel Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspSample:WelcomeLabel Text="Hello" NameForAnonymousUser="Guest" ID="WelcomeLabel1"
runat="server" BackColor="Wheat" ForeColor="SaddleBrown" />
</div>
</form>
</body>
<head id="Head1" runat="server">
<title>WelcomeLabel Test</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<aspSample:WelcomeLabel Text="Hello" NameForAnonymousUser="Guest" ID="WelcomeLabel1"
runat="server" BackColor="Wheat" ForeColor="SaddleBrown" />
</div>
</form>
</body>
View Default.aspx in your browser.
In addition to the WelcomeLabel control's Text property that was explicitly defined in the markup that you copied, you can see from the control instance in the page that it has BackColor and ForeColor properties that you did not define. The WelcomeLabel control gets these and other style-related properties by inheritance from the WebControl base class. In addition, WelcomeLabel can be assigned a skin and be part of a theme without any work on your part.