ASP. NET page value passing Method

Source: Internet
Author: User
Tags http cookie

1. Preface

In traditional ASP applications, the POST method can easily transfer one or more values from one page to another, and the same method can be used in ASP. NET. In ASP. there are many methods to pass values between pages in. NET. The QueryString method, Session method, and Server are used below. the Transfer method, Cookie object method, Application Object method, PostBackUrl attribute method, and @ PreviousPageType command method are used to understand ASP.. NET

2. Use QueryString to pass the value

1) Introduction

QueryString is a simple and many value passing method, but it displays the passed value in the browser address, this method can be used if one or more values with low security requirements or simple structure are passed.

2) create a project

 

 

 

 

File, new, website, create a website project

Select an ASP. NET Website, Click Browse, select the location to save, and click OK

3) coding design

Ø switch back design box

 

Under the code editing box, click design to switch back to the design page of the webpage.

Design the webpage for sending data

 

On the left-side Toolbar of the editing box, select the control to draw the webpage interface.

 

Draw the web page as shown in

Ø Encoding

 

Compile the Button_Click event and create a string variable data with the value web. aspx? Name = the value entered in the TextBox1 control, that is, the value to be passed. The Response. Redirect () method indicates that the page is redirected to another page, that is, Response. Redirect (the page to be transferred. aspx)

Add a webpage

After the file is created, you can view the new file in the project file.

Design the webpage for receiving data

Use the toolbox to draw the webpage for receiving data in the new file

Ø Encoding

 

Request. QueryString is used to obtain parameters. The Request. QueryString statement is added to the page. To obtain the value parameter = ***, this statement returns the value after the equal sign and assigns this value to the Text attribute of the Label1 control, write Label1.Text = Request. queryString ["parameters for Obtaining values"]

Testing

 

Enter text in the text box and click Button

Show text in the text box just entered

3. Use Session variables to pass values

1) Introduction

Session features: the data is stored on the server, any type of data can be stored, the default life cycle is 20 minutes, you can manually set a longer or shorter time, when Session is used to save data and call this data, the returned value is object.

2) modify the webpage code for sending data

Session ["name"] = Text. box1.Text refers to the TextBox. the Text value is written to the Session. Before the Session expires (20 minutes by default), you can use Session ["name"] to obtain its value.

3) modify the webpage code of the received data

Label1.Text = Session ["name"]. toString (); indicates that the value of name in the Session is obtained and assigned to Label1.Text. Because the Session return type is object type, it needs to be converted to a string, that is. tostring ()

Session. Remove ("name") clears the value of the Session name and releases its space.

4) test

Enter text in the text box and click Button

Show text in the text box just entered

4. Use Server. Transfer to pass the value

1) Introduction

Use the Server. Transfer Method to direct data streams from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is object-oriented.

2) modify the webpage code for sending data

Server. Transfer ("web. aspx") indicates to jump to another page. migrating to another page will maintain service resources, instead of simply notifying the Browser Server to change pages and migrate requests.

Public string name

{

Get

{

Retrun TextBox1.Text;

}

}

Set a public attribute for this page. When the name attribute is used, the returned value is data on TextBox1.Text.

3) modify the webpage code of the received data

_ Default wb indicates creating the wb instance variable of the data sending page class

Wb = (_ Default) Context. Handler: gets the object passed from the previous webpage and forcibly converts it to _ Default

Label1.Text = wb. name: Assign the value of name in wb to Label1.Text.

4) test

Enter text in the text box and click Button

Show text in the text box just entered

5. Use Cookie object variables

1) Introduction

Compared with Session, Cookie data is stored on the client

2) modify the webpage code for sending data

HttpCookie cookie = new HttpCookie ("name") instantiates the HTTP cookie object. HttpCookie provides a secure method for creating and operating independent HTTP cookies. name is the variable name for storing data.

Cookie. Value = TextBox1.Text: the input information is assigned to the cookie. Value attribute.

Response. AppendCookie (cookie) Add the cookie to the internal Cookie set

3) modify the webpage code of the received data

Label1.Text = Request. Cookies ["name"]. Value. ToString (): extract the name Value in the Cookie, convert it to a string, and assign it to Label1.Text.

4) test

Enter text in the text box and click Button

Show text in the text box just entered

6. Use the Application object variable

1) Introduction

The scope of the Application object is global, that is, it is valid for all users. This method is not often used because the Application is shared in an Application domain, and all users can change and set its value, therefore, it is generally applied to counters and other places where global variables are needed.

2) modify the webpage code for sending data

Application ["name"] = TextBox1.Text: The value entered in Text Box1.Text is assigned to the Application object. The variable for storing data is named name.

3) modify the webpage code of the received data

Label1.Text = Application ["name"]. ToString (): extract the value of name in Application, convert it to a string, and assign it to Label1.Text.

4) test

Enter text in the text box and click Button

Show text in the text box just entered

7. Run the @ PreviousPageType command.

1) Introduction

This command is a new command in. net 2.0. It is used to process the new cross-page transfer function provided by ASP. NET 2.0 and is used to specify the page on which the cross-page transfer process starts.

2) modify the webpage code for sending data

Remove the Button1_Click event in Button1

 

In the page design box, click

Add the code in the red box in Button1, indicating that after clicking the Button, the page will go to web. aspx

Sets an attribute and returns the control object of a TextBox.

3) modify the webpage code of the received data

Add the above Code to the top, that is, set the address of the send-Back Page

Assign the Text attribute value returned by name on the previous page to Label1.Text

4) test

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Enter text in the text box and click Button

Show text in the text box just entered

8. PostBackUrl attributes

1) Introduction

Similar to the previous method, it can be said that it is another implementation of the previous method. The previous method mainly implements value transfer by directly returning the control. Here we use the query control on the send data page.

2) modify the webpage code for sending data

Delete the above Code

3) modify the webpage code of the received data

Delete the above Code

3) modify the webpage code of the received data

Delete it

Label1.Text = (TextBox) PreviousPage. findControl ("TextBox1 ")). text indicates to query the control whose ID is TextBox1 on the send data page, forcibly convert the control to TextBox, and assign its Text attribute value to Label1.Text.

4) test

Enter text in the text box and click Button

Show text in the text box just entered

9. Solve the Problem

The Default Button is not processed before the Button is clicked. before aspx, the web. aspx. data does not exist at this time. before processing the code in aspx, add a judgment. Use the IsCrossPagePostBack attribute to check whether the request comes from Default. aspx, written on the "Accept data" Page:

After writing, the web. aspx page will jump to the web. aspx page.

 

This article from: http://huobumingbai.blog.51cto.com/1196746/413240 "do not understand the blog-technology .."

 

 

 

 

 

 

 

Related Article

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.