How. NET implements parameter passing between pages

Source: Internet
Author: User
Tags tostring
Page

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"];
}

The

Use 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 to pass a value using the session variable are as follows:
1, add the necessary controls to the page
2, create a button and link button
3 that can return the form, and add the value of the control to the session variable in the Click event of the button or link button
4, using the Response.Redirect method to redirect to another page
5, extract the value of the session on another page, and explicitly clear it when you are sure that the session is not needed
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
//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");
}

The

Use Server.Transfer
method is slightly more complex than the one described above, but it is particularly useful in the transfer of values between pages, and you can use this method to access the exposed values in the form of object attributes on another page, and of course, using this method, You need to write some extra code to create properties so that you can access it on another page, but the benefits of this approach are 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 of
3, create a button and link button that returns a form
4, call the Server.Transfer method in the button click event handler 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 to implement 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;
}

The cross page transfer feature is enabled in ASP.net 2.0, and its features and usage are introduced later!

Passing values between pages

Mode 1:

Add a row to the HTML code for the receiving page: <%@ Reference page = "WebForm1.aspx"%>

WebForm1 fp= (WebForm1) Context.Handler;
This.   Textbox1.text=fp.name; Name is the public variable for the first page


The context provides access to the entire current contexts, including the request object. You can use this class to share information between pages.

Way 2:get Way
On the Send page
public int sum=0;

int i =int. Parse (this. TextBox1.Text) *2;

Server.Transfer ("webform2.aspx?sum=" +i);

Receive page
This. textbox1.text=request["Sum"]. ToString ();
or this. textbox1.text=request.params["Sum"]. ToString ();
This. textbox1.text=request.querystring["sum"];


Method 3: Global variables

Send page:
application["Sum"]=this. TextBox1.Text;
Server.Transfer ("webform2.aspx");

Receive page:
This. Textbox1.text= (string) application["sum"];

Application is essentially a collection of all the files in the entire virtual directory, and the Application object is the best choice if you want to use a variable value throughout the application.

In this case, the session[""] method is identical

Method 4:

Send page:
1. Define static variables: public static string Str= "";
2. Str=this. TextBox1.Text;
Server.Transfer ("webform2.aspx");
Receive page:
1. Introduce the first page of the namespace: using WebApplication1;
2 this. TEXTBOX1.TEXT=WEBFORM1.STR;



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.