Several methods for transferring page values in ASP. NET

Source: Internet
Author: User

I. Currently, there are several methods for passing values on pages in ASP. NET:
1. Form submission,
<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 ):
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 ;}
Setincluthis.txt stadate. Text = value ;}
}
/// <Summary>
/// End Time
/// </Summary>
Public String enddate
{
Get {return this.txt enddate. Text ;}
Setincluthis.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 ):
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) Context. Handler;
Response. Write ("stadate :");
Response. Write (querypage. stadate );
Response. Write ("<br/> enddate :");
Response. Write (querypage. enddate );
}
}

3. If many query pages share a result page, set the method as follows:
In this method, the key lies in the conversion of "querypage = (querypage) Context. Handler;", only the conversion
It can be implemented without depending on a specific page. If you want all query pages to inherit an interface, define a method in this interface.
The only function of the method is to allow the result page to obtain the parameters required for building results, so that multiple pages can share a result page!

1. Define a class and place all query parameters with the class:
/// <Summary>
/// Value to be used on the result page
/// </Summary>
Public class queryparams
{
Private string stadate;
Private string enddate;

/// <Summary>
/// Start Time
/// </Summary>
Public String stadate
{
Get {return this. stadate ;}
Set {This. stadate = value ;}
}
/// <Summary>
/// End Time
/// </Summary>
Public String enddate
{
Get {return this. enddate ;}
Set {This. enddate = value ;}
}
}

2. Interface Definition:
/// <Summary>
/// Define the query interface.
/// </Summary>
Public interface iqueryparams
{
/// <Summary>
/// Parameters
/// </Summary>
Queryparams parameters {Get ;}
}

3. the query page inherits the iqueryparams interface (querypage. aspx ):
/// <Summary>
/// Query page, inherited Interface
/// </Summary>
Public class querypage: system. Web. UI. Page, iqueryparams
{
Protected system. Web. UI. webcontrols. textbox txtstadate;
Protected system. Web. UI. webcontrols. textbox txtenddate;
Private queryparams;
...
/// <Summary>
/// Parameters used on the result page
/// </Summary>
Public queryparams Parameters
{
Get
{
Return queryparams;
}
}
...
Private void btnenter_click (Object sender, system. eventargs E)
{
// Assign a value
Queryparams = new queryparams ();
Queryparams. stadate = this.txt stadate. text;
Queryparams. enddate = this.txt enddate. Text
Server. Transfer ("resultpage. aspx ");
}
}
4. This is also true for other pages.
5. Receiving page (resultpage. aspx ):
Public class resultpage: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
Queryparams = new queryparams ();
Iqueryparams QueryInterface;
// Page for implementing this interface
If (context. handler is iqueryparams)
{
QueryInterface = (iqueryparams) Context. Handler;
Queryparams = QueryInterface. parameters;
}
Response. Write ("stadate :");
Response. Write (queryparams. stadate );
Response. Write ("<br/> enddate :");
Response. Write (queryparams. enddate );
}
}

 

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.