Asp.net control development (1) -- display control content

Source: Internet
Author: User

The display of the asp.net control is naturally inseparable from the display of HTML, CSS, Javascript, and other foreground content. Therefore, the first thing to do when developing a control is to know how to output the content to be displayed on the client.

1. Select base class

All the standard controls in asp.net can be used as the base class. If the control we want to develop is only to enhance the functions of the original standard controls (for example: you can add some follow-up functions to the CheckBox of the TreeView to directly use the standard control as the base class.

Generally, if the developed control cannot be found from the standard, it can be inherited from three classes:

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

The following describes the relationships and differences between the three classes:

Control: it only provides simple rendering and does not support css. For example, Literal control
WebControl: Creates support for the control appearance. Suitable for visual control inheritance, such as: Button
CompositeControl: Composite of multiple derived controls. It is suitable for developing standard controls in application asp.net.

Relationship: Control is the base class of all controls in asp.net, WebControl is inherited from Control, and CompositeControl is inherited from WebControl.

Ii. How to present

Rendering of Control

The rendering in the Control class is implemented through the method Render. Render prototype:

Protected internal virtual void Render (HtmlTextWriter writer ){...}

The HtmlTextWriter writer parameter is provided by the framework that calls the Render method at runtime, so we can rewrite the Render method to Render the content.

HelloWorld example:

Public class HelloWorld: Control {
Protected override void Render (HtmlTextWriter writer)
{
Writer. WriteLine ("Henllo World ");
}
}
After compilation, adding dll files to the new project will show "Hello World ".

Control outputs html content

In the Render method, we can implement the output html Tag and style by using the following enumeration: HtmlTextWriterTag, HtmlTextWriterAttribute, and HtmlTextWriterStyle. HtmlTextWriterTag indicates the Html Tag, HtmlTextWriterAttribute indicates the attribute on the tag, and HtmlTextWriterStyle indicates the style.

Picture example:

Public class PicShow: Control
{
Protected override void Render (HtmlTextWriter writer)
{
Writer. AddStyleAttribute (HtmlTextWriterStyle. TextAlign, "center"

Related Article

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.