Introduction to several methods for passing values on ASP. NET pages

Source: Internet
Author: User

I. Currently, there are several methods for passing values on pages in ASP. NET:

1. Form submission

Reference content is as follows:
<Form action = "target. aspx" method = "post" name = "form1">
<Input name = "param1" value = "1111"/>
<Input name = "param2" value = "2222"/>
</Form>
....
Form1.submit ();
....


This method is implemented in ASP. NET is invalid because ASP. . NET forms are always submitted to their own pages. If you want to submit other pages, special processing is required.
2. <A href = "target. aspx? Param1 = 1111 & param2 = 2222 "> link transfer </A>
Receiving page: string str = Request ["param1"]
3. Session sharing
Sending page: Session ("param1") = "1111 ";
String str = Session ("param1"). ToString ();
4. Application Sharing
Sending page: Application ("param1") = "1111 ";
By page: string str = Application ("param1"). ToString ();
This method is not often used. Because the Application is shared within an Application domain, all users can change and set its value. Therefore, only the counters and other places that require global variables are used.
5. Cookie
6. Response. Redirect () method
Response. Redirect ("target. aspx? Param1 = 1111 & param2 = 2222 ")
Receiving page: string str = Request ["param1"]
7. Server. Transfer () mode.
Server. Transfer ("target. aspx? Param1 = 1111 & param2 = 2222 ")
Receiving page: string str = Request ["param1"]

2. If you need to transmit a large number of parameters between two pages, such as data query pages, use the 1-6 method to transfer the value and the inconvenience, the 7th methods have a unique advantage! However, some settings are required to use this method. The following describes how to use this method:

Take the Data Query page as an example:

On the query page, set the following public attributes (QueryPage. aspx ):

Reference content is as follows:
Public class QueryPage: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. TextBox txtStaDate;
Protected System. Web. UI. WebControls. TextBox txtEndDate;
...
/// <Summary>
/// Start Time
/// </Summary>
Public string StaDate
{
Get {return this.txt StaDate. Text ;}
Set {this.txt StaDate. Text = value ;}
}
/// <Summary>
/// End Time
/// </Summary>
Public string EndDate
{
Get {return this.txt EndDate. Text ;}
Set {this.txt EndDate. Text = value ;}
}
....
Private void btnEnter_Click (object sender, System. EventArgs e)
{
Server. Transfer ("ResultPage. aspx ");
}
}

On the displayed query result page (ResultPage. aspx ):

Reference content is as follows:
Public class ResultPage: System. Web. UI. Page
{
Private void Page_Load (object sender, System. EventArgs e)
{
// Convert to obtain the data entered on the previous page.
QueryPage queryPage = (QueryPage) Context. Handler;
Response. Write ("StaDate :");
Response. Write (queryPage. StaDate );
Response. Write ("<br/> EndDate :");
Response. Write (queryPage. EndDate );
}
}

  • 2 pages in total:
  • Previous Page
  • 1
  • 2
  • Next Page

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.