Asp. NET in HTMLControl and WebControl

Source: Internet
Author: User
Tags define comments html tags implement include classic asp
Asp.net|web compared to previous ASP versions, ASP. NET has been greatly improved. Among these improvements, the main point is the newly introduced server-side controls. Now we're going to work together on the HTMLControl and WebControl in asp.net, two types of server-side controls.

Recall: An example of not using a server-side control

To illustrate the benefits of the server-side controls, we now use the traditional ASP to make a page. The function of this page is to display some comments about this combination based on the user's selected electric guitar and power amplifier combination. This page will be completed as shown in Figure 1:


Figure 1

Select the type of guitar and amp from the combo box and press the information button. The ASP code is judged by the choice of guitar and AMP models, and gives some comments on this combination. Listing 1 shows the traditional ASP code for this page.

When we write the ASP code, we are actually telling the ASP DLL to generate some specific HTML tags and text information. These tags and text are already explicitly included in our ASP code. ASP DLLs Throw
In this example, we have to write a large section of the If...then statement in order to correctly display the options we choose in the combo box. The code looks a bit verbose, but it can implement a feature that is almost impossible to implement with HTML.

Listing 2 is the HTML code that our ASP program sends to the client when it runs, where the <select> tag shows a combo box that shows all of the items we can select, while the highlight bar stays on one of our actual choices.

A fundamental problem here is that the HTTP protocol that transmits HTML is a "connectionless" protocol, so it is also a "stateless" protocol. The service side does not save the state between each request of the client. While we use combo boxes and other controls in our desktop applications, it's easy to keep this state because all the code is under the same roof--only one user clicks on the button on your program.

Web development can be different. Since the web is a "connectionless" protocol based on HTTP, it is possible that every visit to your page comes from a different customer. Therefore, as a web developer, we must write our own code to track the state of each user between different visits. In the example above, we get and parse the query string to the last state of the user before we can select the appropriate <option> and add the "selected" attribute to it.

Wouldn't it be wonderful if the language of our programming provided us with these basic services?

Very fortunate, ASP. NET is the introduction of server-side controls for this purpose.

Asp. Server-side controls in net

The traditional ASP code is a mixture of HTML and executable scripting code. Each time a client submits a request, the server generates a stack of HTML response codes. Unfortunately, with the complexity of the program logic, this ASP code finally became as messy as the unstructured basic program in the cheap programming textbooks of the the early 1980s.

As previously shown, classic ASP Web processing includes manual parsing of form data and processing controls. Asp. NET introduces server-side controls to make access to controls more intuitive. Because HTTP is a "connectionless" protocol, the maintenance of customer status has been a vexing problem. The server-side control can automatically keep the user's state in a different postback (postback) so that we don't have to bother to write similar code again and again.

Using server-side controls for development is much more like our usual desktop development. In desktop development, you can assume that the state of the same control is consistent throughout the entire desktop program's runtime, and that server-side controls allow you to assume the same thing while you're doing web development, although the UI for Web applications is being accessed by thousands of people.

As we mentioned earlier, ASP. NET's server-side controls include two kinds of open: HTMLControl and WebControl. This allows the control to encapsulate a series of cumbersome management operations within, and we only need to deal with the most concerned parts. Unlike traditional ASP, we don't have to embed HTML tags manually in our code, we just use HTMLControl and WebControl these controls, and they help us deal with all of this. They will be able to maintain the state between different sessions, enabling us to finish the work earlier than the traditional ASP programmer!

Now let's start with the HTMLControl.

HtmlControls

Asp. NET, HTMLControl and WebControl are standard CLR based controls. Like all other CLR classes, they also inherit from System.Object objects, and they also have attributes, methods, and proxies and events. Their primary function is to manage controls on a Web page.

The HTMLControl class corresponds to various tag one by one in HTML. For example, when the ASP.net engine discovers that you have defined a button in the ASP code, it wraps it as a HtmlButton class. When you need to access the button in a program, you only have to access the object of the HtmlButton class.

The following table lists the HtmlButton classes that are supported in the current asp.net.

Class name Function Htmlanchorhtml <a>htmlbuttonhtml <button>htmlcontainercontrol represents an HTML server-side control, The control must have an end tag HTMLControl represents an HTML server-side control htmlformhtml <form>htmlgenericcontrol represents a no dedicated. NET HTML server-side control for the framework class htmlimagehtml htmlinputbuttonhtml <input type= button>, <input type= Submit , and the abstract class of the <input type= reset>htmlinputcheckboxhtml <input type= checkbox>htmlinputcontrolhtml input controls, These input controls include <input Type=text&gt, <input type=submit&gt, and <input type= and file> htmlinputfilehtml Type= file>htmlinputhiddenhtml <input type=hidden>htmlinputimagehtml <input type= htmlinputradiobuttonhtml <input type= radio> element on the serverhtmlinputtexthtml <input type= text> and <i Nput type= password>htmlselecthtml <select>htmltablehtml <table> The single HTML <td> and <th> element htmltablecellcollection contained in HtmlTableRow is contained in the HtmlTable HtmlTableCell The collection of table cells in HtmlTableRow contains a single HTML <tr> element contained in HtmlTable HTMLTABLEROWCOLLECTIOntablerow Collection htmltextareahtml <textarea>

Listing 3 shows the "Axe ' n ' Stacks" page written with ASP.net htmlcontrol. Note that the layout of each selection box, button, and image element is very similar to the traditional ASP code. When the ASP.net engine encounters these tags, they map them to the corresponding HTMLControl. As you can see from the code, the Submit button ("Information") is connected to the message processing routine Fetchinfo_click. Note the way Fetchinfo_click manages controls by accessing form fields. It first checks the incoming guitar, amplifier combination, and then fills in the text box control in the corresponding content. It then sets the image control to display the correct picture of the guitar and the amplifier. As shown in Figure 2.


Figure 2

Basically, if you define a control in the code in a traditional ASP, and add the "Runat=server" attribute to it, the ASP. NET will be packaged into a htmlcontrol.

WebControls

Roughly speaking, WebControl and HTMLControl have similar functions. Many WebControl can also be mapped directly to standard HTML controls. However, WebControl provides a more consistent interface than the HTMLControl. For example, sometimes the background color of a htmlcontrol can be modified by the style property, and sometimes by other specialized attributes. For WebControl, however, it is always modified by the BackColor attribute.

WebControl is somewhat different from the way HTMLControl is defined. Listing 4 is the WebControl version of the "Axe ' n ' Stacks" page. Note that the selected combo box is defined as "Asp:dropdownlist" in WebControl and its contents are defined Asp:listitem. Similarly, WebControl uses Asp:button to define the button, and the image and text box controls are also defined as Asp:image and Asp:textbox respectively. If you read the message handling code at the top of the program, you'll also find that many of the properties that the controls use to store the data have changed. For example, the Value property of the HtmlSelect is changed to the DropDownList ' s Selecteditem.text property. In addition, the structure of the program is basically the same as that written with HTMLControl.

more complex WebControls

mentioned earlier, WebControl has a more consistent interface than HTMLControl. Furthermore, one advantage of WebControl than HTMLControl is that WebControl provides a number of more complex but useful controls, including Adrotator,calendar, and DataGrid. The AdRotator control receives some images, such as JPG and GIF files, and maps them to a series of URLs, respectively. Each time the page is refreshed, AdRotator randomly extracts an image to display it. When it is clicked, the page is redirected to the appropriate URL. The Calendar control can display a strong calendar table in the browser, while the DataGrid control is equivalent to a mini spreadsheet.

Conclusion

Today, web-based development can be attributed to HTTP requests and replies, and in many cases these responses need to return the control's markup back to the browser. When programming directly with ASP, you often need to spend a lot of time and effort to keep the controls consistent in different requests. After all, the world is no longer a desktop application! Asp. NET control can help you solve a lot of tumultuous trivial things, make you feel like in MFC or Visual Basic development, so that your application distributed on the global computer, at all times by thousands of users access.

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.