Summary of several methods for implementing page value passing through ASP. NET

Source: Internet
Author: User

The three methods are QueryString, Session, and Server. Transfer.

Send. aspx:
 Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
Request. Redirect ("Default2.aspx? Username = honge ");
}

Receive. aspx:
Copy codeThe Code is as follows:
String username = Request. QueryString ["username"]; // you can obtain the parameter value.

Method 2:
Post. Send. aspx
Receive. aspx
Copy codeThe Code is as follows:
String username = Ruquest. Form ["receive"];

Method 3:
Send. aspx through session:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
Session ["username"] = "honge ";
Request. Redirect ("Default2.aspx ");
}

Receive. aspx:
Copy codeThe Code is as follows:
String username = Session ["username"]; // you can obtain the parameter value.

Method 4:
Use Application send. aspx:
Copy codeThe Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
Application ["username"] = "honge ";
Request. Redirect ("Default2.aspx ");
}

Receive. aspx:
Copy codeThe Code is as follows:
String username = Application ["username"]; // you can obtain the parameter value.

Method 5:
Use Server. Transfer send. aspx:
Copy codeThe Code is as follows:
Public string Name
{
Get {
Return "honge ";
}
}
Protected void button#click (object sender, EventArgs e)
{
Server. Transfer ("Default2.aspx ");
}

Receive. aspx:
Copy codeThe Code is as follows:
Send d = Context. Handler as send;
If (d! = Null)
{
Response. Write (d. Name); to obtain the parameter value.
}

If you can use this method in asp.net 2.0: PreviousPage
Copy codeThe Code is as follows:
PreviousPage d = Context. Handler as PreviousPage;
If (d! = Null)
{Response. Write (d. Name); // you can obtain the parameter value.
}

You can also use: send. aspx:
Receive. aspx:
String name = PreviousPage. Name; to obtain the parameter value.


If MasterPage is used in your page, the PreviousPage passed by Server. Transfer is invalid. I don't know why. If MasterPage is used, it is best to use Session or Context. Items ["username.
QueryString
Using QuerySting to pass values between pages is already a very old mechanism. The main advantage of this method is that it is very simple to implement, however, its disadvantage is that the passed value is displayed on the address bar of the browser (Insecure), and the object cannot be passed, however, this method is a good solution when the number of transmitted values is small and the security requirements are not high. The steps for using this method are as follows:
◆ Use the control to create a web form (form)
◆ Create button and link button that can return the form
◆ Create a character variable for saving the URL in the button or link button click event
◆ Add the QueryString parameter to the saved URL
◆ Use Response. Redirect to the saved URL above
The following code snippet demonstrates how to implement this method:
Source Page code:
Copy codeThe Code is as follows:
Private void button#click

(Object sender, System. EventArgs e)
{
String url;
Url = "anotherwebform. aspx? Name = "+ TextBox1.Text +" & email = "+ TextBox2.Text;
Response. Redirect (url );
}

Target Page code
Copy codeThe Code is as follows:
Private void Page_Load
(Object sender, System. EventArgs e)
{
Label1.Text = Request. QueryString ["name"];
Label2.Text = Request. QueryString ["email"];
}

Use Session Variables
Using the Session variable is another way to pass values between pages. In this example, we store the value in the control in the Session variable and then use it on another page, to transfer values between different pages. However, it should be noted that storing too much data in the Session variable will consume a lot of server resources, so you should be careful when using the session. Of course, we should also use some cleanup actions to remove unnecessary sessions to reduce unnecessary resource consumption. The general steps for transferring values using Session variables are as follows:
◆ Add necessary controls to the page
◆ Create button and link button that can return the form
◆ In the Click Event of a button or link button, add the control value to the session variable.
◆ Redirect to another page using the Response. Redirect Method
◆ Extract the session value from another page. Clear the value explicitly when determining that this session is not required.
The following code snippet demonstrates how to implement this method:
Source Page code:
Copy codeThe Code is as follows:
Private void button#click
(Object sender, System. EventArgs e)
{
[Url = file: // textbox1] file: // textbox1 [/url] and textbox2 are webform
[Url = file: // controls] file: // controls [/url]
Session ["name"] = TextBox1.Text;
Session ["email"] = TextBox2.Text;
Server. Transfer ("anotherwebform. aspx ");
}

Target Page code:
Copy codeThe Code is as follows:
Private void Page_Load
(Object sender, System. EventArgs e)
{
Label1.Text = Session ["name"]. ToString ();
Label2.Text = Session ["email"]. ToString ();
Session. Remove ("name ");
Session. Remove ("email ");
}

Use Server. the Transfer method is a little more complex than the method described above, but it is particularly useful in inter-page value Transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious. In general, this method is concise and object-oriented. The entire process of using this method is as follows:
◆ Add necessary controls to the page
◆ Create the Get attribute process of the returned value
◆ Create button and link button that can return the form
◆ Click the button in the event handler to call the Server. Transfer Method and Transfer it to the specified page.
◆ On the second page, we can use the Context. Handler attribute to obtain the reference of the previous page instance object. Through it, we can use the value of the control to access the previous page.
The following code comprehensively implements the above steps:
Source Page code:
Add the following code to the page
Copy codeThe Code is as follows:
Public string Name
{
Get
{
Return TextBox1.Text;
}
}
Public string EMail
{
Get
{
Return TextBox2.Text;
}
}

Then call the Server. Transfer Method
Copy codeThe Code is as follows:
Private void button#click
(Object sender, System. EventArgs e)
{
Server. Transfer ("anotherwebform. aspx ");
}

Target Page code:
Copy codeThe Code is as follows:
Private void Page_Load
(Object sender, System. EventArgs e)
{
[Url = file: // create] file: // create [/url] instance of source web form
WebForm1 wf1;
[Url = file: // get] file: // get [/url] reference to current handler instance
Wf1 = (WebForm1) Context. Handler;
Label1.Text = wf1.Name;
Label2.Text = wf1.EMail;
}

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.