In asp.net, the get and post methods are used for transferring values between pages. Other methods, such as session, appliction, and cookie, are rarely used, it is only used in special circumstances.
1. Get(That is, use QueryString for explicit transfer)
Method: follow the url with parameters.
Features: simple and convenient.
Disadvantage: the string can contain a maximum of 255 characters, and the data is leaked in the url.
Applicable data: simple, small, and key data.
Applicability: pass data to the user and to another target page. It is often used to transfer data between two pages.
Usage: for example, what is the url suffix? UserID = ..., Go to the target page. The target page can use Request. QueryString ["InputText"] on the servo side to obtain the specified parameter value.
Example: (1) a. aspx
| The code is as follows: |
Copy code |
Private void button#click (object sender, System. EventArgs e) { String s_url; S_url = "B. aspx? Name = "+ Label1.Text; Response. Redirect (s_url ); } |
(2) B. aspx
| The code is as follows: |
Copy code |
Private void Page_Load (object sender, EventArgs e) { Label2.Text = Request. QueryString ["name"]; } |
2. Post
Method: common method. Use form to submit.
Features: The most common method. A common technique is to submit hidden data in a hidden domain by form.
Applicable data: massive data, including file upload.
Applicability: same as the Get method
Usage: submit after the action target is specified in the form client, use server. Transfer (url) for submission in the asp.net servo end, and obtain it using Request. Form ["FormFieldID"] in the servo end.
Instance
| The code is as follows: |
Copy code |
<Form id = "form1" action = "GetUserInfo. aspx" method = "post"> <Table align = "center" cellpadding = "0" cellspacing = "10px" border = "0" style = "width: 320px; border: 1px solid gray;"> <Tr> <Td colspan = "2" class = "title" align = "center"> Change password </td> </Tr> <Tr> <Td class = "left"> enter the new password: </td> <Td class = "right"> <Input type = "password" id = "userPwd" name = "userPwd" style = "width: 175px"/> </Td> </Tr> <Tr> <Td class = "left"> & nbsp; Confirm password: </td> <Td class = "right"> <Input type = "password" id = "verifyPwd" name = "verifyPwd" style = "width: 175px"/> </Td> </Tr> <Tr> <Td colspan = "2" style = "text-align: center"> <Hr/> <Input type = "hidden" name = "username" value = "<% = the value of the variable defined in the background when reading the database is %>"/> </td> <Input type = "submit" value = "modify"/> <Input type = "reset" value = "reset"/> </Td> </Tr> </Table> </Form> |
There is a way to set values based on POST and GET.
| The code is as follows: |
Copy code |
Public object string GetValueFromPage (string inputName, int type) { HttpContext rq = HttpContext. Current; Object TempValue = ""; If (type = 1) { If (rq. Request. Form [inputName]! = Null) { TempValue = rq. Request. Form [inputName]; } } Else if (type = 0) { If (rq. Request. QueryString [inputName]! = Null) { TempValue = rq. Request. QueryString [inputName]; } } Return TempValue; } |
When you submit the request to the GetUserInfo. aspx page, you can obtain the value in GetUserInfo. aspx. CS.
The following describes the disadvantages and advantages of other values passing.
Cookie
Method: classic method for storing data on the client.
Disadvantages: low security, limited by client settings, a site only has 20 cookies, each with a capacity of 4096 bytes.
Session
Method: store user data on the servo end.
Features: in asp.net, you can set the session storage mode, location, and whether the session id is stored based on cookies.
Objects can be stored directly.
Disadvantages: potential failure in asp.net
Cache
Method: store user data in the servo data cache.
Features: greatly improves efficiency. Objects can be stored directly.
Appliction
Method: store the data here, which is equivalent to a global variable.
Features: objects can be stored directly. Shared data of the entire site
ViewState
Method: asp.net special mechanism, used to restore the page status.
Feature: the controls on the page and their stored data are serialized in a hidden domain named _ ViewState.
Disadvantage: it is in HTML and has low security. Encryption and verification can be set, but the data volume increases greatly and the efficiency may be affected.
Static
Method: store data in static variables.
Features: improves efficiency.
Disadvantages: poor use may cause data disorder between users or pages, causing great risks. We recommend that you assign a value only once. You cannot change this value for a single user.
Summary:The value transfer method between pages that I have not described in detail above does not seem to be suitable for transferring values between common pages and pages on our commonly used WEB pages. It is only suitable for special occasions.