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 attribute (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"); // Note: Use ResultPage. aspx to receive the passed parameters.
}
}
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 = (QueryPage) Context. Handler; // Note: reference the page handle.
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:
The key to this method is the conversion of "querypage = (querypage) Context. Handler;", which can be implemented only when the conversion does not depend on a specific page.
If you want all query pages to inherit an interface and define a method in this interface, the only function of this method is to obtain the parameters required for building the result on the result page, you can share multiple pages with one result page!
1. Define a class and use it to place all query parameters: (*. CS)/** // <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 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 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 );
}
}
Iii. Reasons for this article:
Because a data query is required at work, too many parameters are annoying. Previously, the Session was passed. After the parameters sent from the Session are used up, the Session needs to be cleared, before using the Session, You have to determine whether the Session exists. It is extremely cumbersome. I think there should be a simpler way to transfer parameters between pages. Therefore, you can search for the Session online, finally, we found this method to implement parameter transfer between pages.
No. please correct me!
========================================================== ========================================================== =
First, let's look at the HttpContext class:
The System. Web. HttpContext class inherits from System. Object and is understood by class name, that is, the Http context class.
This class encapsulates all HTTP-specific information about a single HTTP request. This class provides reference to the HttpContext object of the current HTTP request for classes that inherit the IHttpModule and IHttpHandler interfaces. This object provides access to the internal Request, Response, and Server objects of the Request.
Common public attributes of the HttpContext class include:
Application to obtain the HttpApplicationState object for the current HTTP request.
Current to obtain the HttpContext object for the Current HTTP request.
Handler, which gets or sets the IHttpHandler object for the current HTTP request.
Items to obtain a set of key values that can be used to organize and share data between IHttpModule and IHttpHandler during HTTP requests.
Request to obtain the HttpRequest object for the current HTTP Request.
Response: gets the HttpResponse object for the current HTTP Response.
Server to obtain the HttpServerUtility object that provides a method for processing Web requests.
Session to obtain the HttpSessionState instance for the current HTTP request.
You can obtain the current System. Web. HttpContext object through the Context attribute of the Page class.
Next let's look at the Server. Transer () method:
You can use the Server attribute class of the Page class to Transfer to another Page, such as Server. Transfer ("NewPage. aspx"), to jump to the new Page,
Use Server. the client URL does not change when the Transfer () is redirected to the page, but the new page is executed and output on the server, therefore, you can obtain the objects and form data transmitted on the request page and query strings on the new page.
Assume that the current page is FormerPage. aspx (Class Name: FormerPage), and the new page to jump to is NewPage. aspx.
The code for redirecting from FormerPage. aspx is as follows:
Private void btnToNewPage_Click (object sender, System. EventArgs e)
{
ArrayList list = new ArrayList (3 );
List. Add ("This list ");
List. Add ("is ");
List. Add ("FormerPage to see .");
Context. Items ["FormerPageList1"] = list;
Server. Transfer ("NewPage. aspx ");
}
In the new page (NewPage. aspx) Page_Load () event, use the following code to obtain the transmitted data:
If (! IsPostBack)
{
Try
{
FormerPage former = (FormerPage) Context. Handler;
TxtFromParentPage. Text = former. ClassName; // obtain the common attributes of ClassName defined in FormerPage.
// Obtain the ArrayList: Context. Items ["FormerPageList1"] added in the Context Dictionary of FormerPage.
// Obtain the Contex dictionary and force convert the Data Type:
ArrayList list = Context. Items ["FormerPageList1"] as ArrayList;
DataSet ds = former. GetDataSet (); // call the GetDataSet () Public method defined in FormerPage.
DataGrid1.DataSource = ds;
DataGrid1.DataBind ();
}
Catch
{
Response. Write ("Error get data from parent transfer page! ");
}
}
Note that the IHttpHander object of the current Http request is obtained through the Context. Hander attribute and is forcibly converted to a FormerPage object:
FormerPage former = (FormerPage) Context. Handler;
You can call the Public attributes and methods of this class. You can also call the Context Dictionary Item (Dictionary Item) added in FormerPage ).
It is worth noting that Server is used. transer transmits page data and uses Context. handler is used to receive data. Only when the page is loaded for the first time can the instance on the previous page be obtained correctly. In postback, the instance on the current page is obtained.
For example, in NewPage. in aspx, you can obtain the FomerPage object when loading for the first time. However, an exception is thrown when you try to obtain the FormerPage during sending back because the request page has changed during sending back, it is not a request sent by FormerPage, but a request sent by NewPage. we can add the following code to Page_Load () to determine which page the Http request is sent:
String path = Context. Request. Path;
Response. Write ("<script> alert ('request from:" + path + "'); </script> ");
In addition, Server. transer () has an overloaded method Server. trasfer (string newpage, bool preserveForm), the second parameter is used to specify whether to retain HttpRequest. form and HttpRequest. queryString set. If true, the Form and QueryString of the original page are still valid on the new page and can be called. for example:
String str = "Value of Textbox:" + Request. Form ["TextBox1"] + "<br> ";
From: http://hi.baidu.com/yuyueye0/blog/item/b0e64ca8d1c16ffa1f17a2f6.html