Asp. NET to achieve page transfer value of several methods summary _ practical skills

Source: Internet
Author: User

These three methods are: Querystring,session and Server.Transfer.

Pass send.aspx via URL link address:

Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
Request.redirect ("Default2.aspx?username=honge");
}

Receive.aspx:
Copy Code code as follows:

String username = request.querystring["username"];//so that the parameter values can be obtained.

The second method:
By post method. Send.aspx
Receive.aspx
Copy Code code as follows:

String username = ruquest.form["Receive"];

The third method:
Through session send.aspx:

Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
session["username"] = "Honge";
Request.redirect ("default2.aspx");
}

Receive.aspx:
Copy Code code as follows:

String username = session["username"];//so that the parameter values can be obtained.

The fourth method:
Through application send.aspx:
Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
application["username"] = "Honge";
Request.redirect ("default2.aspx");
}

Receive.aspx:
Copy Code code as follows:

String username = application["username"];//so that the parameter values can be obtained.

The Fifth method:
Through Server.Transfer send.aspx:
Copy Code code as follows:

public string Name
{
get {
return "Honge";
}
}
protected void Button1_Click (object sender, EventArgs e)
{
Server.Transfer ("default2.aspx");
}

Receive.aspx:
Copy Code code as follows:

Send d = context.handler as send;
if (d!= null)
{
Response.Write (D.name) so that the parameter values can be obtained.
}

If you can use this in asp.net 2.0: through the PreviousPage
Copy Code code as follows:

PreviousPage d = Context.Handler as previouspage;
if (d! = null)
{Response.Write (D.name);//This will get the parameter values.
}

You can also use this: send.aspx:
Receive.aspx:
String name = Previouspage.name, so you can get parameter values.


If your page uses the masterpage words Server.Transfer transmission previouspage is invalid, does not know this is what reason. So in the use of masterpage words, it is better to use the session or context.items["username" to achieve.
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:
Create a Web Form with a control (form)
Create buttons and link buttons that return the form
Create a character variable to save a URL in the Click event of a button or link button
Add the querystring parameter to the saved URL
Use Response.Redirect to redirect to the URL saved above
The following code fragment demonstrates how to implement this method:
SOURCE page code:

Copy Code code as follows:

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
Copy Code code as follows:

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:
Add the necessary controls to the page
Create buttons and link buttons that return the form
In the Click event of a button or link button, add the value of the control to the session variable
To redirect to another page using the Response.Redirect method
Extract the value of the session on another page and explicitly clear it when you are sure you don't want to use it
The following code fragment demonstrates how to implement this method:
SOURCE page code:
Copy Code code as follows:

private void button1_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 Code code 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");
}

Using the Server.Transfer method is a little 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 properties, and of course, in this way you need to write some extra code to create properties so that they can be used on the other page Face access to it, 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:
Add the necessary controls to the page
To create a Get property procedure for a return value
Create buttons and link buttons that return the form
Call the Server.Transfer method in a button-click event handler to the specified page
In 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
Copy Code code as follows:

public string Name
{
Get
{
return TextBox1.Text;
}
}
public string EMail
{
Get
{
return TextBox2.Text;
}
}

Then call the Server.Transfer method
Copy Code code as follows:

private void button1_click
(object sender, System.EventArgs e)
{
Server.Transfer ("anotherwebform.aspx");
}

Target page code:
Copy Code code 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.