How does. Net implement parameter transfer between pages?

Source: Internet
Author: User
Question: How does. Net implement parameter transfer between pages?
Source: blog from risingsoft
Reason for adding to favorites: Learning

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:
1. Use the control to create a web form (Form)
2. Create buttons and link buttons that can return the form
3. Create a character variable to save the URL in the button or link button click event.
4. Add the querystring parameter to the saved URL.
5. Use response. Redirect to redirect to the saved URL above
The followingCodeThe snippet demonstrates how to implement this method:
Source Page code:
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:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}

using session variables
using session variables is another way to pass values between pages. In this example, we store the values in the control in the session variables, 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:
1. Add necessary controls on the page
2, create a button and link button that can return the form
3. Add the control value to the session variable in the Click Event of the button or link button.
4, use response. the redirect method redirects to another page
5. Extract the session value from another page, to explicitly clear it
the following code snippet demonstrates how to implement this method:
Source Page code:
private void button#click
(Object sender, system. eventargs e)
{< br> // textbox1 and textbox2 are webform
// controls
session ["name"] = textbox1.text;
session ["email"] = textbox2.text;
server. transfer ("anotherwebform. aspx ");
}< br> Target Page code:
private void page_load
(Object sender, system. eventargs e)
{< br> label1.text = session ["name"]. tostring ();
label2.text = session ["email"]. tostring ();
session. remove ("name");
session. remove ("email");
}

Use server. transfer
This 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:
1. Add the necessary controls on the page
2. Create the get attribute process of the returned value
3, create a button and link button that can return the form
4. Click the event processing button Program to call server. transfer the transfer method to the specified page
5. On the second page, we can use context. the handler attribute is used to obtain the reference of the previous page instance object. With the handler attribute, you can use the value of the control on the previous page to access the value of the control.
the following code comprehensively implements the code of the above process:
Source Page code:
Add the following code to the page
Public string name
{< br> Get
{< br> return textbox1.text;
}< BR >}

Public String email
{
Get
{
Return textbox2.text;
}
}
Then call the server. Transfer Method
Private void button#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;
}

Cross-page transfer is enabled in ASP. NET 2.0. Its functions and usage will be introduced later!

Pass value between pages

Method 1:

Add a line in the HTML code of the receiving page: <% @ reference page = "webform1.aspx" %>

Webform1 fp = (webform1) Context. Handler;
This. textbox1.text = FP. Name; // name is the public variable on the first page.

Context provides access to the entire current context (including the request object. You can use the information between such shared pages.

Method 2: Get
On the send page
Public int sum = 0;

Int I = int. parse (this. textbox1.text) * 2;

Server. Transfer ("webform2.aspx? Sum = "+ I );

Receiving 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 ");

Receiving page:
This. textbox1.text = (string) application ["sum"];

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

Here the session [""] method is the same

Method 4:

Send page:
1. Define static variables: public static string STR = "";
2. Str = This. textbox1.text;
Server. Transfer ("webform2.aspx ");
Receiving page:
1. Introduce the namespace on the first page: 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.