Passing values between two ASP.net pages

Source: Internet
Author: User
Tags tostring
Asp.net| page
Introduction
Asp. NET provides an excellent event-driven programming model that allows developers to simplify the overall design of the application, but this also creates some of its inherent problems, for example, using the traditional ASP, we can easily deliver values between pages by using the Post method, the same thing, The asp.net of the event-driven programming model is not so easy, of course, we still have some ways to do the same. This article will try to solve this problem using different possible methods, but it is expected that this article will contain the use of querystring,session variables and the server. Transfer method to implement the value transfer between pages.

Using QueryString
Using querysting to pass values between pages is a very old mechanism, the main advantage of this method is very simple to implement, but its disadvantage is that the value passed is displayed in the browser's address bar (unsafe), but also can not pass the object, However, this approach is a good solution when the value is low and the security requirements are not high. The steps to use this method are as follows:
1, create a Web Form with a control (form)
2, create buttons and link buttons to return the form
3, create a character variable to save the URL in the Click event of a button or link button
4, add the QueryString parameter in the saved URL
5, use Response.Redirect to redirect to the URL saved above
The following code fragment demonstrates how to implement this method:
SOURCE page code:
private void button1_click
(object sender, System.EventArgs e)
{
string URL;
Url= "Anotherwebform.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
Response.Redirect (URL);
}
Target page code:
private void Page_Load
(object sender, System.EventArgs e)
{
label1.text=request.querystring["Name"];
label2.text=request.querystring["email"];
}

Using the session variable
Using the session variable is another way to pass a value between pages, in this case we have the value in the control in the session variable and then use it in another page to implement the value transfer between different pages. However, it should be noted that in the session variable storage too much data will consume more server resources, in the use of sessions should be cautious, of course, we should also use some clean-up action to remove some unnecessary session to reduce the unnecessary consumption of resources. The general steps for passing values using session variables are as follows:
1, add the necessary controls to the page
2, create buttons and link buttons to return the form
3, in the Click event of a button or link button, add the value of the control to the session variable
4, redirect to another page using the Response.Redirect method
5, extract the session value on another page and explicitly clear it when you are sure that it is not necessary to use the session
The following code fragment demonstrates how to implement this method:
SOURCE page code:
private void button1_click
(object sender, System.EventArgs e)
{
TextBox1 and TextBox2 are WebForm
Controls
session["Name"]=textbox1.text;
session["Email"]=textbox2.text;
Server.Transfer ("anotherwebform.aspx");
}
Target page code:
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");
}

Using Server.Transfer
This method is slightly more complicated than the one described above. However, it is particularly useful in the transfer of values between pages, and you can use this method to access the exposed values in the same way as object attributes, and of course, you need to write some extra code to create properties so that you can access it on another page. However, the benefits of this approach are also obvious. Overall, the use of this approach is both concise and object-oriented. The whole process of using this method is as follows:
1, add the necessary controls to the page
2, create a Get property procedure that returns a value
3, create buttons and link buttons to return the form
4, call the Server.Transfer method in the button click event handler to transfer to the specified page
5, on the second page, we can use the Context.Handler property to get a reference to the previous page instance object, through which you can use the value of the control that accesses the previous page.
The following code synthesizes the code that implements the procedure above:
SOURCE page code:
Add the following code to the page
public string Name
{
Get
{
return TextBox1.Text;
}
}

public string EMail
{
Get
{
return TextBox2.Text;
}
}
Then call the Server.Transfer method
private void button1_click
(object sender, System.EventArgs e)
{
Server.Transfer ("anotherwebform.aspx");
}
Target page code:
private void Page_Load
(object sender, System.EventArgs e)
{
Create instance of source Web Form
WebForm1 WF1;
Get reference to current handler instance
wf1= (WebForm1) Context.Handler;
Label1.text=wf1. Name;
Label2.text=wf1. EMail;
}

Summarize
This article describes the use of different methods to achieve the ASP.net page value transfer, these three methods are: Querystring,session and Server.Transfer, we should repeatedly understand the similarities and differences between several methods
I hope this article will give you useful help until you use it freely in your code!


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.