Do not drag control ASP. NET -- General handler, Drag Control asp.net

Source: Internet
Author: User
Tags website server

Do not drag control ASP. NET -- General handler, Drag Control asp.net

I am used to a series of Web controls provided by Microsoft. It is very convenient to use. Recently I have read a new method to implement the same functions without dragging controls, whether the Drag Control is good or not. As you learn more in the future, the answer will be revealed slowly. I hope you will keep paying attention to it! Today, let's look at the general processing program.

1. General Processing Program (HttpHandler) introduced

Let's start with the process of opening a website. For example:


Process details: Use webForm to develop a simulated login page. When the user opens the login page, fill in the corresponding user information: user name and password, and click Login, the browser sends the user name and password to the website server. The website server sends the asp.net handler responsible for processing the login request to process the request. The Handler determines whether the user name and password are correct, then, return the processing result to the browser.

The essence of surfing the Internet: surfing the internet is a Web page that is described in Html format transmitted from the server to the browser. Each request brings back a new page. The images, js, and css in the page are in a separate request.

Note: We know that Html files, css files, and javascript files are parsed by the browser. In the above login process, there are basically three parts: sending requests (requests) to the server) -rendering the server into html (processing)-returning to the browser and drawing it to the client (response ).

Ø HttpHandler

From the above introduction, we know the interaction between the browser and the server request-processing-response, then HttpHandler (General handler) plays the role of the server processor, and the entire operation is:

The browser collects user operations and sends a request to the server -- the server processes the request and renders the result into an html file -- the browser responds to the server operation and parses and displays the returned results.

Ø instance

Example 1

In this case, you must manually enter the address, parameter name, and age in the default directory of the iis website.

The httphandler. ashx code is as follows:

<Strong> <span style = "font-family: Microsoft YaHei; font-size: 14px;"> using System; using System. collections. generic; using System. linq; using System. web; namespace handler {// <summary> // summary of TestHandler /// </summary> public class TestHandler: IHttpHandler {public void ProcessRequest (HttpContext context) {// context. response. contentType = "text/plain"; context. response. contentType = "text/html"; string action = context. request ["name"]; string isVIP = context. request ["isVIP"]; int age = Convert. toInt32 (context. request ["age"]); context. response. write ("<font color = 'red'> Hello" + action + "</font>"); context. response. write ("<font color = 'green'> me this year" + age + "</font>");} public bool IsReusable {get {return false ;}}}} </span> </strong>

The effect is as follows:


Example 2

We found that every time you manually enter a parameter, it is easy to see errors, so we can use html forms to automatically submit them to the server for processing.

The modification code is as follows:

HTML page:

<Strong> <span style = "font-family: Microsoft YaHei; font-size: 14px;"> <! DOCTYPE html> 

General handler: Same as above.

Detailed operation steps:

1. Html form <form> can automatically submit parameters to the server (get is url, post is style), without the need for users to spell URLs themselves. Action specifies to whom the form content is submitted. That is, action and method specify the HttpHandler to which the request is submitted and the submission method.

2. the browser submits data to the server. The submitted data forms (such as input, select, and textarea) are placed in the form. The form sets the page to which the form is submitted through the action attribute, to obtain the table item value on the server side, you must set the name attribute for the form element in HTML.

3. Note that id is used to operate dom for js, and name is used for submission to the server. The Id and name must be unique.

4. The server side (httphandler here) uses context. Request ["username"] to obtain the submitted attribute value based on the name of the form item.

Note:

1. When the submit button is clicked, the browser "extracts" the values in the text box and other spaces filled by the user and sends them to the server, instead of the server, which reads the information filled by the user.

2. labels that can be submitted to the server have the following features:

A. It can only be Input, textarea, or select tags. Only these tag users can modify the values, such as <lable> <p> and <font>. They only display the values and are not necessary to submit them to the server. When input = submit, only the value of the clicked button is submitted. The content of the submitted form is submitted to the server. (In addition, there is a hidden control input = "den", which is a server control and will be submitted to the server because it is mapped to an input control on the browser, type = "hidden ".)

B. Only the values of the value Attribute of the three tags are submitted to the server. The title and type attributes of the input tag are displayed only and cannot be modified by the user.

C. You must set the name attribute for the tag. To submit the value of the tag to the server, you must set the name value for the tag, when submitted to the server, it will be submitted to the server in the form of a "name = value" key-value pair. name is used in the Service and id is used for js dom operations, for radiobutton, the same name is a group, and the value of the selected radiobutton is submitted to the server.

D. Put it in the form label. Only those placed in the form label can be submitted to the server. The input and other labels outside Form are ignored.

2. Difference Between Get and post

1. Get transmits the form value through the url, and the value of the form field cannot be seen through the url of post;

2. The amount of data transmitted by Get is limited. If a large amount of data is transferred, you cannot use get, such as type = "file" or <textarea> to publish a large article. Post does not have this restriction. However, the post cannot be restored in other users through the url. This will display an error.

3. Get url data format, followed by "?" "Because the client may submit multiple key-value pairs to the server, use" & "to separate key-value pairs. If the url contains Chinese or special characters, you need to encode it.

Ø example display

Enable network traffic capture with F12 on ie9

The Get request header contains the data sent during the request. The request body is blank:


The data sent during the Post request is transferred from the header string to the Request body.


3. http protocol and message request protocol

1. Connection: the data transmission channel between the browser and the server. Generally, the request is closed, and http does not maintain a connection, or is short connection. The advantage is that the number of client concurrent requests processed by the server can be enhanced; The disadvantage is that the server processing speed is reduced because the connection establishment speed is slow.

2. Request: The Request information sent by the browser to the server, including the Request type, data, and browser (client) Information (language, browser version, and IP address.

3. Response: The data returned by the server after processing the browser request, including whether the request is successful and the error code.

Ø message parsing (requesting a page such as csdn's own)

Request

1. Get/HTTP/1.1 indicates to request page in GET mode to the server, and Http1.1 is used.

2. Accept-Language indicates the Language type supported by the browser. User-Agent indicates the version of the browser.

3. Referer: The Source Page and the page to which the Source Page (image, js, css file, etc.) belongs.

Response

Response Code: "200" OK indicates that the request response is successful;

"302": Found indicates redirection. Response. Redirect () enables the browser to request the redirection address again. The Redirect request method is Get.

"404": Not Found indicates that the requested page is Not Found.

"500": indicates an internal server error

 

Content-Type: text/html: indicates the Type of the returned data. The server uses this attribute to tell the client the Type of the response data. In this way, the browser performs different processing based on the Type of the returned data, image/gif is displayed, text/plain is displayed, and plain text code is displayed. You can try to compare the differences between text/html and text/plain. Click it.

4. Summary

I learned how the browser and the server work, which makes it easier for us to understand the code. Previously, we used to drag and drop controls for development. It seems very convenient. In fact, when dragging controls, the control will generate something else for us.

Here we use html + httphandler to achieve interaction between the browser and the server. In fact, it is equivalent to a web form. We know that web controls can be submitted to the server because runat = "server", however, we can use form forms to submit to handler for processing at this time. At this time, hanler is equivalent to the server. Its benefits also need to be learned in the future, and the Learning continues. Please continue to pay attention to it ~



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.