Custom Web server controls

Source: Internet
Author: User
Document directory
  • Control Painting Process
  • Use custom Web server controls

Walkthrough of this section in MSDN: http://msdn.microsoft.com/zh-cn/library/yhzc935f (v = vs.100). aspx

The introduction by Microsoft is sufficiently detailed. Here I just want to briefly describe how to customize Web server controls. The steps are as follows:

  1. Create a control class to inherit from WebControl or other controls to be extended (such as Button and TextBox)
  2. Add the required attributes and methods, and then rewrite the Render and AddAttribute methods so that they can be displayed on the client.
  3. After the function is complete, compile the project and add the configuration items of Web. Config to be referenced so that it can work properly.

Taking the drill in MSDN for example, we can create an empty web project, add a Controls directory (not required for the hierarchy), and create a new Web Control class, the Code is as follows:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Security.Permissions;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace SampleCustomWebControl.Controls{    [        AspNetHostingPermission(SecurityAction.Demand,            Level = AspNetHostingPermissionLevel.Minimal),        AspNetHostingPermission(SecurityAction.InheritanceDemand,            Level = AspNetHostingPermissionLevel.Minimal),        DefaultProperty("Text"),        ToolboxData("<{0}:WelcomeLabel runat=\"server\"> </{0}:WelcomeLabel>")    ]    public class WelcomeLabel : WebControl    {        [            Bindable(true),            Category("Appearance"),            DefaultValue(""),            Description("The welcome message text."),            Localizable(true)        ]        public virtual string Text        {            get            {                string s = (string)ViewState["Text"];                return (s == null) ? String.Empty : s;            }            set            {                ViewState["Text"] = value;            }        }        protected override void RenderContents(HtmlTextWriter writer)        {            writer.WriteEncodedText(Text);            if (Context != null)            {                string s = Context.User.Identity.Name;                if (s != null && s != String.Empty)                {                    string[] split = s.Split('\\');                    int n = split.Length - 1;                    if (split[n] != String.Empty)                    {                        writer.Write(", ");                        writer.Write(split[n]);                    }                }            }            writer.Write("!");        }    }}

There is no change to the full Microsoft code. It should be noted that the above features are mainly used during design.

We recommend that you inherit from WebControl instead of Control, because the WebControl class derives from Control and adds style-related attributes, such as Font, ForeColor, and BackColor. It is closer to our usage habits.

Control Painting Process

In this example, because only the Text attribute is set and displayed as the control content, you only need to override RenderContents.

When the control is drawn, the Render method is called first, and then the RenderBeginTag, RenderContents, and RenderEndTag methods are called in the Render method. The renderinintag and RenderEndTag methods are used to draw the start and end of a tag. They depend on the TagName field. As long as the TagName field of the class is overwritten, there is no need to rewrite these two methods.

To add some attributes to the control, you must override the AddAttributesToRender method.

Use custom Web server controls

Compile the project as a. dll file and reference it in the web project.

Add the webConfig Configuration:

      <pages>        <controls>          <add tagPrefix="cc" namespace="SampleCustomWebControl.Controls" assembly="SampleCustomWebControl" />        </controls>      </pages>

Use:

<Cc: WelcomeLabel ID = "lblWelcome" runat = "server" Text = "welcome"> </cc: WelcomeLabel>

The Text attribute can also be dynamically changed in the program:

LblWelcome. Text = "Welcome, server time:" + DateTime. Now. ToString ("yyyy-MM-dd HH: mm: ss ");

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.