Article content
This article demonstrates how to develop a server-side control by creating the simplest Server Control.
The content of this article is organized in the MSDN programming guide. The original Article address is in the resource at the end of the article.
This document creates a simple server control named RedLabel. It is used in the following ways:
This label will output its Text property value in red to the page. Running result:
Procedure
Create a blank solution. In this solution, create a class library project named MyControl. Create a new server-side control in the class library named RedLabel. :
Open the RedLabel class and modify the code of the entire class:
Using System; using System. collections. generic; using System. componentModel; using System. text; using System. web; using System. web. UI; using System. web. UI. webControls; namespace MyControl {[DefaultProperty ("Text")] [ToolboxData ("<{0}: RedLabel runat = server>
")] Public class RedLabel: Label {// rewrite the RenderContent function, and output the control content protected override void RenderContents (HtmlTextWriter output) {// you can set your own Text attributes (inherited from the Label class) value output in red format. write ("" + Text + "");}}}
Now the custom label control has been compiled.
Configure Assembly Properties
1. Open the Assembly property file under the class library project:AssemblyInfo. cs,:
1. Add the following code at the beginning of the file:
using System.Web.UI;
2. Add the following code at the end of the file:
[assembly: TagPrefix("MyControl", "f")]
MyControl is the namespace name. F is the label prefix of the custom control.
Code Description
If the control is to be rendered as invisible elements in the client browser (such as hidden elements orMetaIs derived from System. Web. UI. Control. The WebControl class is derived from Control and adds style-related attributes, such as Font, ForeColor, and BackColor. In addition, the custom control writes text to the response stream by rewriting the RenderContents method.
Use custom controls on the page
To use custom controls on the page, you must register them in advance to map the control prefix and namespace, in this way, you can find the implementation class corresponding to the tag through the tag name. Two registration methods are available:
1. Use the @ Register command on the page, as shown in the following example:
<%@ RegisterAssembly="ServerControl" TagPrefix="aspSample” Namespace="ServerControl"%>
2. Specify the tag prefix/namespace ing in the Web. config file. This method is useful if custom controls are used on multiple pages of a Web application. The following example shows a Web. config file that specifies the ing between the namespace MyControl and the tag prefix f in the Assembly MyControl.
Test controls
Create a web project under the solution, create an apsx page in a web project, and introduce custom controls to the page (note that the control prefix must be registered ). For example, the following page:
<%@ Page Language="C#"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="MyControl.Web._Default" %><%@ Register Assembly="MyControl"TagPrefix="f"Namespace="MyControl" %>
Running Effect
Resource:
MSDN create server controls: http://msdn.microsoft.com/zh-cn/library/yhzc935f (v = vs.100). aspx