Difference between post and get (also about the way to pass values between pages)

Source: Internet
Author: User
[Convert] the difference between post and get (also about the way to pass values between pages)


There are two request methods: Post and get.

If their differences come from the principle and involve the details of the HTTP transmission protocol, this article will only discuss the representation.

Everyone knows the following differences:
1. When the post object transmits data, it does not need to be displayed in the URL, but the get method must be displayed in the URL.
2. The size of post data transmission can reach 2 MB. The get method is limited by the URL Length and can only transmit about 1024 bytes.
3. post, as its name implies, is to transfer data to the server segment, and get is to get data from the server segment. the reason why get can also transmit data is to design and tell the server what data you actually need. the post information is used as the content of the HTTP request, while get is transmitted in the HTTP header.

There are two methods for Form: Post and get. The difference between the method and the method in page value passing is the three points mentioned above.

Let's take a look at the POST method.

This method should be followed in the ASP EraProgramThere were a lot of people dealing with it, because there was no current viewstate at that time. To restore the original state of each page, you had to post the page to yourself, and then assign a value one by one. now all these trivial things make viewstate work. therefore, the actions that post pages to itself have been forgotten by Asp.net programmers to some extent, so the post has been ignored by most of them. This is a double-edged sword of technological progress, at the same time, it brings convenience to your eyes.

Viewstate must be included in the <form runat = "server"> form. If the form contains the "runat =" server "", the viewstate will be post to other pages, why? The old guy said, My viewstate is to save the current page status, you want to go to other pages, he said, no, ** unknown parameters. what if I want to post a form? There are four options.

1. create a new form on the page. Do not add the runat = "server" flag. Of course, the controls in this form do not need to use viewstate to pass values. when a button event in another form with runat = "server", manually call the submit () function of the new form.

Transfer pageCodeAs follows:

<! -- HTML code -->
< Form ID = " Form1 " Method = " Post " Runat = " Server " >
< Input ID = " Btntransfer " Type = " Button " Onclick = " Post (); " Runat = " Server " >
< Input type = " Text " Runat = " Server " ID = " Sourcedata " >
</ Form >
< Form ID = " Forpost " Method = " Post " >
< Input type = " Text " Runat = " Server " ID = " Sourcedata2 " >
</ Form >

<! -- Script code -->
< Script Language = " Javascript " >
Function Post ()
Forpost. Action = " Destinationpage. aspx " ;
Forpost. Submit ();
</ Script >

Receiving page

StringA=Request. Form ["Sourcedata2"]. Tostring ();

2. using the session value, assigning values on a page, sharing among other pages, this method is also widely used. Individuals do not tend to use this method. I am afraid it will cause disorder and disorder of the session value, the session is too tired to store some public things.

3. Pass the value through context. The value to be passed to other pages must be included in context before the page is transferred. The sample code is as follows:

Transfer page

// Triggered when a button is clicked
Private   Void Btntransfer_serverclick ( Object Sender, eventargs E)
Context. items ["Sourcedata"]=Sourcedata. value;
Server. Transfer ("Destinationpage. aspx");

Receiving page

StringA=Context. items ["Sourcedata"]. Tostring ();

4. Use server. Transfer.
This method has been used in three methods, but you can create an attribute for the value to be passed to the called page on the call page (of course, you can directly set it to public ), in this way, you can access other pages.

Transfer page

// Value to be transferred
System. Web. UI. htmlcontrols. htmlinputtext sourcedata;

Public   String Getsourcedata
ReturnSourcedata. value;

// Transfer page
Server. Transfer ( " Destinationpage. aspx " );

Receiving page

PrivateSourceclass sourcepage;

Sourcepage=(Sourceclass) Context. Handler;
StringAA=Sourcepage. getsourcedata;

The above is the POST method for transferring data on different pages.
The following is the get method.
I often use transfer pages.

String AA = Sourcedata. value;
String Bb = Sourcedata. value;

String URL = " Destinationpage. aspx? Parameter1 = " + AA + " & Parameter2 = " + BB;
Response. Redirect (URL, False );

Receiving page

StringAA=Request. querystring ["Parameter1"]. Tostring ();
StringBb=Request. querystring ["Parameter2"]. Tostring ();

As Response. Redirect (URL, false) In False All Response. End () The disaster caused by this method is written False This is fine, because the default value is True . I have switched to other pages, and I am not allowed to terminate the response of the original page, BT!

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.