Asp. NET pages and several ways to transfer values between

Source: Internet
Author: User

1, QueryString

When the form on the page sends the request data to the page in a Get way, Web server puts the request data into a qeury_string environment variable, and the Qeuerystring method takes the corresponding value from the variable.

First set up two WebForm, add two text boxes for WebForm1 and Webform2,webform1 respectively to enter the client input, a button to navigate to the page to pass the value of the WebForm2, add a text box on the destination page to display the value passed over.

WebForm1 button click Code:

String url = "Webform2.aspx?name=" + txtName.Text + "&age=" + txtage.text;

Response.Redirect (URL);

WebForm2 button click Code:

TextBox1.Text = "Name is:" + request.querystring["name"] + "Age is:" + request.querystring["ages"];

Advantages: Easy to use

Cons: Low security, the passed value will be displayed in the URL of the browser, the object cannot be passed;

Knowledge Involved: HTTP protocol, request and response objects, get and post differences, redirect and method differences;

2. Session

Put the required value in the session variable, and then use it in another variable, the session is stored on the server side, the session variable to store too many variables will consume more server resources, it is also timely to clean up the unnecessary session variables.

Advantages: Can transfer complex objects, data size is not limited

Cons: As a global variable, easy to operate by mistake

WebForm1 button click Code:

session["name"] = txtName.Text;

Session["age"] = Txtage.text;

Server.Transfer ("webform2.aspx");

WebForm2 button click Code:

TextBox1.Text = "Name is:" + session["name"]. ToString () + "Age is:" + session["ages"]. ToString ();

Session.remove ("name");

Session.remove ("Age");

Related to: Session object, server object, transfer method

3, Application

Application is valid throughout the application life cycle, similar to using global variables, which are global variables shared by all users, and the session is a global variable unique to each user and is typically used to record user information.

WebForm1 button click Code:

Application.Lock ();

application["name"] = txtName.Text;

Application["age"] = Txtage.text;

Server.Transfer ("webform2.aspx");

Application.UnLock ();

WebForm2 button click Code:

TextBox1.Text = "Name is:" + application["name"]. ToString () + "Age is:" + application["ages"]. ToString ();

Involves: Application object, how to go from one page to another; Change of global variable, lock

4. Cookies

Cookies are used to store information about the user in the client, which transmits information through the HTTP header, can contain only the value of the string, and the cookie collection of the Request object allows all cookies to be obtained by the browser.

Pros: Easy to use, user saved user state

Cons: Being criticized for collecting user privacy

WebForm1 button click Code:

HttpCookie name = new HttpCookie ("name1");

HttpCookie age = New HttpCookie ("Age1");

Name. Value = txtName.Text;

Age. Value = Txtage.text;

RESPONSE.COOKIES.ADD (name);

RESPONSE.COOKIES.ADD (age);

Server.Transfer ("webform2.aspx");

WebForm2 button click Code:

TextBox1.Text = "Name is:" + request.cookies["name1"]. Value.tostring () + "Age is:" + request.cookies["Age1"]. Value.tostring ();

Related to: Cookie,response.appendcookie and Response.Cookies.Add differences, cookie Preservation

5, Server.Transfer

The above four ways are often used in ASP, this method is only in the net, it is used to go from the current page to the new ASPX page, the server side executes a new page and output, in the new page through the Context.Handler to get the previous page passed the various types of values, form data, QueryString; When Server.Transfer is used, the current page terminates execution and the stream executes into another page, but the new page still uses the reply flow created by the previous page.

(1) Server.Transfer is done on the server side, so the URL address in the client browser will not change, and the URL address in the client browser will change as the client completes and the new page processing request is made to the server side.

(2) The Server.Transfer is done on the server side, without requiring the client to make requests, reducing the client's request to the server side.

(3) Server.Transfer can only jump to the page specified in the local virtual directory, that is, the page in the project, and Response.Redirect is flexible enough to jump to any URL address.

(4) Server.Transfer can upload various types of values from the previous page to the new page, Response.Redirect can only use the URL with parameters or combined with the above four ways to upload various types of values to the new page.

Advantages: Direct in the server end multiplicity-oriented, easy to use, reduce the client to server-side request, you can pass the values of various data types and the value of the control.

Disadvantage: The URL address in the client browser is not changed and can cause some unexpected problems in the new page. For example, if the source page and the destination page are not in the same virtual directory or its subdirectories, the use of relative path of the picture, hyperlink will lead to an error point.

It is important to note that getting these values must be the first time a new page is loaded to correctly get the various data types of the previous page or the values of the controls. At a later postback, you will not be able to get the various data types of the previous page or the value of the control because the current instance of the page is now available. So you need to use the IF (!) in the Page_Load () event of the new page (destinationwebform.aspx). IsPostBack) The code that gets the value of the previous page is included to get the values, form data, QueryString of the various data types passed by the previous page.

Server.Transfer the second parameter, if true, indicates that the value of form and querrystring on this page continue to be valid on the new page

WebForm1 button click Code:

ArrayList myList = new ArrayList (3);

Mylist.add ("first");

Mylist.add ("second");

Mylist.add ("third");

context.items["destlist"] = myList;

CONTEXT.ITEMS.ADD ("Newcontext", "Hello");

Server.Transfer ("Webform2.aspx", true);

WebForm2 button click Code:

if (! IsPostBack)

{

Try

{

WebForm1 FormPage = (WebForm1) Context.Handler;

ArrayList list = (ArrayList) context.items["Destlist"];

TextBox1.Text = "Name is:" + Formpage.name + "Age is:" + formpage.age

+ "" + context.items["Newcontext"]. ToString () + "First:" + list[0];

}

Catch

{

Response.Write ("error");

}

}

Related to: Ispostback,context

Asp. NET pages and several ways to transfer values between

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.