ASP. NET page value passing Method

Source: Internet
Author: User

Method 1:

In the HTML of the receiving page Code Add a row to the file: <% @ 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.



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;

 

The other three most common values

ASP. NET web forms provides developers with an excellent event-driven development mode. However, this simple applicationProgramThe development mode brings us some small problems. For example, in the traditional ASP application, you can use the POST method to easily transfer one or more values from one page to another (Request ()/request. form ()/request. querystring (), using the same method in ASP. net. Here, we can solve this problem in other ways. ASP. NET provides three methods for us. One is to use querystring to transmit the corresponding value, the other is to transmit the corresponding value through the session variable, and the other is to use the server. the transfer method.

I. querystring

Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button_click (Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+ textbox1.text +" & Email = "+ textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.

Ii. Use session Variables

Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button_click (Object sender, system. eventargs E)
{
Session ["name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
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 ");
}
Run the command to view the passed result.

Iii. Use server. Transfer

Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void (Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get {return textbox1.text ;}
}
Public String email
{
Get {return textbox2.text ;}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load (Object sender, system. eventargs E)
{
// Create a webform instance
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.email;
}

These three methods are common.

 

The value transfer problem in method 1 above

Q: How does one use URL to pass values between pages in ASP? For example, "index. asp? AAA = China East 5 City + shuixiang 6-day tour"
Because there is a plus sign in the string "China East 5 City + shuixiang 6-day tour", which is in conflict with the keyword of the string connector of the ASP system, the AAA value of the variable passed in will become "China East 5 City ", what are your solutions?
Solution:

1. server. urlencode ("China East 5 City + shuixiang 6-day tour ")

2. urlencode
The urlencode method applies URL encoding rules, including escape characters, to a specified string.

Syntax
Server. urlencode (string)

Parameters
String
Specifies the string to encode.
Example
The following script

<% Response. Write (server. urlencode ("http://www.microsoft.com") %>

Produces the output

HTTP % 3A % 2f % 2 fwww % 2 emicrosoft % 2 ECOM

3. index. asp? AAA = % 2B water township 6-day tour in China East 5

4. Define some special characters corresponding to "+", "*", and "#".
For example, replace (string, "+", "code01 ")

Then, on the other page, replace (string, "code01", "+") is used to replace it. Then, you can define some special characters corresponding to "+", "*", and "#".
For example, replace (string, "+", "code01 ")

Then, on the other page, replace (string, "code01", "+") is used.

Pass reference between pages

There are manyArticleWe will discuss how to pass references between two pages. If the object on your page is set to public, you can pass them between pages.

Use
Server. Transfer can replace response. Redirect.
Example:

------------- In page a codebehind:

Public class pagea: system. Web. UI. Page
{
Public System. Web. UI. webcontrols. textbox textbox1;
Public System. Web. UI. webcontrols. Button button1;

// Standard page code (page_load, etc)
//....
//....

Private void button#click (Object sender, system. eventargs E)
{
Server. Transfer ("B. aspx ");
}
}

------------- In page B codebehind:

Private void page_load (Object sender, system. eventargs E)
{
Pagea myapage = context. handler as pagea;
String textboxfrompagea = myapage. textbox1.text;
}

Because all objects (A and B) are active on the server when server. Transfer is used, you can reference anything.

Slightly modify B. aspx (provided by uestc95 ):

Page mypage = (PAGE) context. Handler;
String textboxfrompagea;
Textboxfrompagea = (textbox) mypage. findcontrol ("textbox1"). text;

In this way, the protected type can be used normally in A. aspx.

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.