ASP. NET advanced programming basics Article 1-Basic Introduction

Source: Internet
Author: User

From today on, I will write a series like this. Of course, I will introduce it from the simplest aspect!

  1. Web applications and websites

(1) What is the difference between WebApplication and WebSite? WebSite exists to be compatible with the habits of developers transferred from ASP. It is easy to use. For example, you do not need to create a namespace, after the cs code is modified, you can see the changes without restarting (neither WebSite nor WebApplication need to be restarted to modify aspx), but it is not conducive to engineering development. For example, it is not easy to detect code errors, code is not named. There is no difference in development technology, but development and debugging habits are different.

  1. Asp.net development based on ashx

(1) Create an Html page named hello1.Htm.

1 <form action = "hello. ashx "> 2 3 <input type =" text "name =" username "/> 4 5 <input type =" submit "value =" submit "/> 6 7 </form>

 

(2) create a [General handler] Hello1.ashx, which is written in ProcessRequest:

1 string username = context.Request["username"];2 3 context.Response.Write("hello world," + username);

 

(3) The ProcessRequest method is called whenever a user requests to access the ashx page. request to obtain the visitor's Request parameters, and then use context in ProcessRequest. response sends data back to the browser. When ProcessRequest ends, the server completes the access service for the browser.

(4) the browser submits data to the server. The submitted data forms (such as input, select, and textare) are placed in the Form. The Form is submitted to that page through the action attribute setting, to obtain the value of a form item on the server side, you need to set the Name attribute for the form element in HTML. Note that the ID is used for JS and Dom operations, and the name is submitted to the server, use context on the server. request ["UserName"] to obtain the submitted attribute value based on the name of the form item. response. write outputs the HTML content after processing to the browser.

  1. A review of Jquery code

1 $(function(){2 3         $(“#TextBox”).mouseover(function(){4 5                $(this).CSS(”Color”,”Red”);6 7 });8 9 });

 

Note: The function of this code segment is to set the font color in the control to red when the mouse leaves the control.

  1. "Back" to submit the original version of the page

(1) For the request, the returned content is the same. The page is saved as an Htm template text with some placeholders to be filled in. When you first enter the page, you can directly access ashx, read the Html template, set the placeholder to be filled in to null, and then output it to the browser.

(2) Add a hidden field to Form to distinguish whether to directly enter the page for the first time or click Submit to re-enter ashx; <input type = "Hidden" name = "ispostback" value = "true"> if you can read ispostback = true from the Request, it means you click submit and then enter ashx again, this is the first time you enter ashx.

(3) ASP. NET to convert the Web virtual path (/images/1.jpg) to the full disk path (d: // www. mysite. the images/1.jpg) method is HttpContext. current. request. mapPath ("/1/ 2.htm ").

(4) Implementation idea: In ProcessRequest, ispostback is first read from the Request. If true is read, the template is loaded when the Request is submitted, replace the placeholder with the calculated value. Otherwise, the placeholder in the template is cleared and output to the browser.

(5) When entering Hello2.ashx, the content is directly output to the browser. The user fills in the value in the output content, and then clicks submit. The server will know that the content is submitted back. (Postback)

(6) It is not a matter of course that the value entered in the text box is displayed after the form is submitted. Developers help read the submitted value and render it. This is the relationship between asp.net and cs. Use aspx to rewrite this program and use ispostback and other attributes for comparison.

(7) http is a request and response model. The server will not read the web page of the browser, and the data submitted by the client web page will be obtained.

  1. Get and Post

(1) You can also set the method attribute of form to specify the form submission method. get (default) transmits the form value through URL, and the form value transmitted by post is hidden in the http message, not seen in the URL.

(2) Difference between get and post: get transmits form values through URL, post does not see the value of form fields through URL, and get delivers limited data, if you want to pass a large amount of data, you cannot use get, such as: type = "file" to upload an article, type = "password" to pass a password or <textarea> to post a large article, there is no limit on post, post will prompt the browser to resubmit the form, and get will not. If the post form is retyped in the address column, it will not prompt to resubmit, because you have not re-submitted the data by re-clicking the address bar.

(3) get url data format. After the Server File name is followed "?", Because the client may submit multiple key-value pairs to the server, the key-value pairs are separated by "&". If the url contains Chinese characters and special characters, the URL must be encoded.

(4) The form field is submitted to the server only when the name is set (you can see it clearly in gei mode ). If you set the name for the submit button, the value of the button will be submitted to the server.

Verification Code:

1 string fullpath = context. server. mapPath ("Hello.htm"); // obtain the full file path 2 3 string content = System. IO. file. readAllText (fullpath); 4 5 // context. response. write (content); 6 7 string ispostback = context. request ["ispostback"]; 8 9 if (ispostback = "true") 10 11 {12 13 context. response. write ("Submit to enter"); 14 15} 16 17 else18 19 {20 21 context. response. write ("go directly"); 22 23} 24 25 <form action = "hello1.ashx"> 26 27 <Input type = "hidden" name = "ispostback" value = "true"/> <! -- This is a hidden field --> 28 29 Name: <input type = "text" name = "UserName"/> 30 31 <input type = "submit" value = "submit"/> 32 33 </form>

 

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.