Understanding of ASP. NET controls (finishing)

Source: Internet
Author: User

Web server controls and HTML server controls

Generally, when developing Web applications using ASP. NET, you are always dealing with controls, which can be said to be ubiquitous. In fact, web form pages (. aspx pages) are also controls.

ASP. NET controls are divided into web server controls (Web controls) and HTML server controls (HTML controls), all of which run on the server side. What is the difference between the two?

HTML tags, such as <A> and <input>, cannot be controlled by programs.
JavaScript and other programming languages can be controlled. In the ASP. NET program, we can add
Runat = "server", you can turn the corresponding HTML tag into an HTML server control, so that you can control these HTML tags programmatically. Reference (http://www.dvbbs.net/tech/net/2006051268338.asp
):

As for the differences between HTML tagging and HTML controls, html
The control is run on the server, and the HTML annotation is run on the client. Specifically, when the ASP. NET web page is executed, it will check whether the annotation has runat
Attribute. If the annotation is not set, the HTML annotation will be treated as a string and sent to the string stream for delivery to the client. The browser on the client will explain the annotation.
Note that the runat = "server" attribute is set. The Page Object puts the control in the controller, and the server code can control the control.
The execution result of the HTML control is converted to HTML annotation, and then sent to the client as a string stream for explanation.

The Web server control is a component used to define the web application user interface on the web forms page. It is a basic element of the web forms programming model. Although the Web Server Control is rendered as HTML, however, their object models do not necessarily reflect the HTML syntax.

1. The Asp.net Server Control provides a more unified programming interface. For example, each Asp.net Server Control has the text attribute.

2. Hide the differences between clients. In this way, programmers can focus more on their business without considering whether the client browser is IE, Firefox, or a mobile device.

3. The Asp.net server control can be saved to viewstate, so that the page can be saved when the page is uploaded back from the client to the server or downloaded from the server to the client.

4. Different event processing models: HTML tagging and HTML control event processing are on the client page, while Asp.net server controls are on the server.

Create a custom Server Control

I. Brief Introduction:

A user control or server control is generally inherited from the system. Web. UI. control class or
System. Web. UI. webcontrols. webcontrol class. For example, the web user control (. ascx page) inherits from
System. Web. UI. usercontrol class, which inherits from system. Web. UI. templatecontorl
The latter inherits from the system. Web. UI. control class, and the Web form page (. ASPX page) also inherits from
System. Web. UI. templatecontorl
Class. Why are there more classes in the middle? These intermediate classes are mainly used to provide shared attributes, methods, and events. All functions cannot be placed in the control
Class. The templatecontorl class provides a set of basic functions for the page class and usercontrol class. The control class is for all
The ASP. NET Server Control provides shared attributes, methods, and events. It serves as a custom ASP. NET developer.
The main class used to send source code to the server controls (why do we need to say "Main classes" because other interfaces are often required to complete more functions when developing these controls ). We usually use the most
The Textbox Control inherits from the system. Web. UI. webcontrols. webcontrol class. The latter also inherits from
System. Web. UI. control class.

Control does not have any functions for the user interface (UI. If you want to create a project without a UI
Control, or other controls that present their own UI, are derived from control. If you want to create a control with a UI
Any control derived from the system. Web. UI. webcontrols namespace that provides an appropriate starting point for custom controls.

The system. Web. UI. webcontrols namespace contains HTML
Marked classes, such as textbox controls and ListBox controls. The namespace also contains classes that do not present on the webpage but support data operations, such
Sqldatasource class and objectdatasource class. Other controls (such as the gridview control and detailsview)
Control) supports data display and editing. The webcontrol class is used as the base class for many classes in the system. Web. UI. webcontrols namespace.

Ii. Example:

1. In an msdn example, create a control that inherits from the control class and has no UI function:

// Mycontrol. CS
Using system;
Using system. Web. UI;

Namespace democontrol
{
Public class mycontrol: system. Web. UI. Control
{
Protected override void render (htmltextwriter writer)
{
If (hascontrols () & (controls [0] Is literalcontrol ))
{
Writer. Write ("<H2> your message :");
Controls [0]. rendercontrol (writer );
Writer. Write ("</H2> ");
}
}
}
}

Then register the control on the ASPX page and use:

// Test. aspx
<% @ Page Language = "C #" %>
<% @ Register tagprefix = "UC" namespace = "democontrol" assembly = "Demo" %>

<UC: mycontrol runat = "server">
<Asp: literal runat = "server" text = "http://pcvc.net"/>
</UC: mycontrol>

Running result:

<H2> your message: </H2>
Http://pcvc.net

2. msdn example:

Namespace spancontrol
{
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;

// Renders the following HTML:
// <Span onclick = "alert ('hello');" style = "color: red;"> Custom contents </span>

Public class mywebcontrol: webcontrol
{
Public mywebcontrol (): Base (htmltextwritertag. span)
{}

[System. Security. permissions. permissionset (system. Security. permissions. securityaction. Demand, name = "fulltrust")]
Protected override void addattributestorender (htmltextwriter writer)
{
Writer. addattribute (htmltextwriterattribute. onclick, "alert ('hello ');");
Writer. addstyleattribute (htmltextwriterstyle. color, "Red ");
Writer. addstyleattribute (htmltextwriterstyle. cursor, "Pointer ");
Base. addattributestorender (writer );
}

[System. Security. permissions. permissionset (system. Security. permissions. securityaction. Demand, name = "fulltrust")]
Protected override void rendercontents (htmltextwriter writer)
{
Writer. Write ("Custom contents ");
Base. rendercontents (writer );
}
}
}

Register the control on the ASPX page and use:

// Spancontrol. aspx
<% @ Page Language = "C #" %>
<% @ Register tagprefix = "UC" namespace = "spancontrol" assembly = "Demo" %>

<UC: mywebcontrol runat = "server"/>

Running result:

<Span onclick = "alert ('hello');" style = "color: red; cursor: pointer;"> Custom contents </span>

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.