AspNet Control Development (1) --- Getting Started Introduction

AspNet provides us with a lot of control, however, many times we feel these controls are not enough, would like to customize according to their own needs some controls, for example, want to achieve can be entered in the DropDownList control, AspNet did not provide us with controls, then you need to write custom controls to meet our needs. This series of articles intended to be a development of AspNet controls entry-level introduction, if you want to know more detailed and in-depth reference may be "in-depth analysis to develop ASP.NET 2.0 controls" and "vertical cut ASP.NET 3.5 controls and components to develop technology" Both this book.

Closer to home, we usually develop custom controls will create a class library project, after you've written the code compiler will generate a dll file, this dll file that controls the development of our own. Suppose now already have a control named HelloWorld.dll, in accordance with the following method can be used in the control of our project.

1 Right-click in the toolbox -> Add tab. Tab can be named, for example: MyControls

2 Right-tab area newly added in -> select items. After selecting the file HelloWorld.dll OK, you see the controls have been added to the toolbox, as shown below:

2010-02-06_221522

3 Use custom controls and use common AspNet controls, directly onto the page can be. After the drag control to the page, the source view will add the following code:

<%@ Register Assembly="ControlsDemo1" Namespace="ControlsDemo1" TagPrefix="cc1" %>

 <cc1:HelloWorld ID="HelloWorld2" runat="server">
 </cc1:HelloWorld>

The first line of the above code is registered for the control, TagPrefix prefix attribute specifies the controls, cc1 is the default prefix, we can also customize to your liking prefix, as follows:

<%@ Register Assembly="ControlsDemo1" Namespace="ControlsDemo1" TagPrefix="oec2003" %>
    <oec2003:HelloWorld ID="HelloWorld1" runat="server">
    </oec2003:HelloWorld>

<% @ Resgiter /> command except in a single page, we can also can be configured to pass the global use in web.config. web.config configuration is as follows:

<system.web>
  <pages>
    <controls>
      <add assembly="ControlsDemo1" namespace="ControlsDemo1" tagPrefix="oec2003"/>
    </controls>
  </pages>
</system.web>

The above brief introduction to how to use a custom already written, and know that writing a custom control usually is to create a class library project. Now take a look at the specific implementation of a custom control.

Implement a custom control, we need to choose a base class, the base class controls the most basic functions required, the following three classes is to develop a control common base class.

System.Web.UI.Control
System.Web.UI.WebControls.WebControl
System.Web.UI.WebControls.CompositeControl

Control: base class for controls development, all the controls are inherited directly or indirectly class. Class scalability and flexibility of the strongest.

WebControl: This class inherits from the Control class, in addition to all the attributes Control, it also provides features such as layout styles. More suitable for higher control of the layout and appearance of the style requirements. Control and the same is applicable to the development of a single control.

CompositeControl: a control to the base class provides AspNet2.0. If you want to create a combination of existing controls controls combined, it can be inherited, such inherits WebControl and implements INamingContainer interface.

If we are just one of AspNet existing controls are simple to expand, we can use any of the existing control as the base class.

Control most basic and most flexible, then the first to implement the Control class is the base class for a simple HelloWorld control. Create a new project in a class HelloWorld, inherited Control, and override the Render method, as follows:

public class HelloWorld:Control
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.WriteLine("hello world!");
    }
}

Compiled in accordance with the method described above is added to the toolbox, and then drag the page will show Hello world after running text on the page. Now for the above code to do hello world changes so that the text display area plus a border and background color, as follows:

public class HelloWorld:Control
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.WriteLine(@"<div style=""width:200px;height:100px;
                border:1px solid #4DA9C2;background-color:#C3D9FF"" >");
        writer.WriteLine("hello world!");
        writer.WriteLine(@"</div>");
    }
}

After operating results:

2010-02-09_224710

Although the effect came out, but the code is to use the direct output html code and inline styles, this of course is not good, not good to capture and generate an error to the client html code format is not good. This problem can be solved by us HtmlTextWriterAttribute, HtmlTextWriterTag, HtmlTextWriterStyle three enumeration. Look at the code below:

public class HelloWorld:Control
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#4DA9C2");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#C3D9FF");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "200px");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);
        
        writer.WriteLine("hello world!");

        writer.RenderEndTag();
    }
}

After operating results and the same as above. And view the page's source code can be seen in this way have a standard generated html indent, and the code is consistent with xhtml standard, it is recommended to make use of in this way instead of directly to spell html code.

Now we WebControl used as a base class to implement the above results, it needs to be rewritten and AddAttributesToRender RenderContents TagKey properties and methods of Span default TagKey is, we need to be rewritten so that Div Div, code is as follows:

protected override HtmlTextWriterTag TagKey
{
    get
    {
        return HtmlTextWriterTag.Div;
    }
}

The method used to set AddAttributesToRender div style and properties

protected override void AddAttributesToRender(HtmlTextWriter writer)
{
    base .AddAttributesToRender (writer);
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
    writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#4DA9C2");
    writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#C3D9FF");
    writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "200px");
    writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
}
RenderContents method is used to render the content, the content here is "hello world!", So only one line of code, of course, be some other controls.
protected override void RenderContents(HtmlTextWriter writer)
{
    writer.WriteLine("hello world!");
}

Make use of the complete code WebControl base class:

public class HelloWorld:WebControl
{
    protected override HtmlTextWriterTag TagKey
    {
        get
        {
            return HtmlTextWriterTag.Div;
        }
    }
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base .AddAttributesToRender (writer);
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#4DA9C2");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#C3D9FF");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "200px");
        writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "100px");
    }
    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.WriteLine("hello world!");
    }
}

This paper describes the use of a custom control, and then talk about a simple implementation uses a different class to do custom base class, it should be said to be walked into the door to the development of controls, and on CompositeControl and other content will be more in-depth described later.

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2011/05/22/2053722.html

Guess you like

Origin blog.csdn.net/weixin_34204722/article/details/93496109