Page Pass Value

Source: Internet
Author: User

A useful method of passing values between pages (Context.Handler), using the description of the current page value in ASP.
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 kind of party is in ASP. NET is not valid because ASP. NET forms are always submitted to their own pages, and special handling is required if you want to submit to a page.
2, <a href= "target.aspx?param1=1111¶m2=2222" > Link address transfer </A>
Receive page: string str = request["param1"]
3. Session Sharing
Send page: Session ("param1") = "1111";
On the receiving page, string str = Session ("param1"). ToString ();
4, Application Sharing
Send page: Application ("param1") = "1111";
By Collection page: String str = Application ("param1"). ToString ();
This method is not used very often, because application is shared in an application domain, and all users can change and set their values, so only apply the counters, etc. where global variables are required.
5. Cookies
6, Response.Redirect () mode
Response.Redirect ("target.aspx?param1=1111¶m2=2222")
Receive page: string str = request["param1"]
7, Server.Transfer () mode.
Server.Transfer ("target.aspx?param1=1111¶m2=2222")
Receive page: string str = request["param1"]
Second, if the two pages need a large number of parameters to pass, such as data query pages, using 1-6 method of transmission value and its inconvenience, and the 7th method has a unique advantage! However, the use of this method requires a certain setting, now briefly describe how the method is used:
Take the query data page for example:
In the query page, set the following public properties (querypage.aspx): Common 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.txtStaDate.Text;}
Set{this.txtstadate.text = value;}
}
/**//**//**////<summary>
End time
</summary>
public string EndDate
{
get{return this.txtEndDate.Text;}
Set{this.txtenddate.text = value;}
}

private void Btnenter_click (object sender, System.EventArgs e)
{
Server.Transfer ("resultpage.aspx"); Note: Use Resultpage.aspx to receive the passed arguments.
}
}
On the Display Query results page (resultpage.aspx):
public class ResultPage:System.Web.UI.Page
{
private void Page_Load (object sender, System.EventArgs e)
{

Convert to get the data you entered on the previous page
Querypage querypage = (querypage) Context.Handler; Note: Referencing a page handle

Response.Write ("Stadate:");
Response.Write (Querypage.stadate);
Response.Write ("<br/>enddate:");
Response.Write (Querypage.enddate);
}
}
Third, if there are many query pages to share a result Page Setup method:
In this way the key is "querypage Querypage = (querypage) Context.Handler; conversion, which can be implemented only if the conversion is not dependent on a particular page.
If you have all the query pages inherit an interface, define a method in the interface, the only role of the method is to let the results page to get the parameters required to build the results, you can achieve multi-page sharing a result page operation!

1, first define a class, use this class to place all query parameters: (*.cs)/**////<summary>
Values to use in the results 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>
Defines the query interface.
</summary>
public interface Iqueryparams
{
/**//**//**////<summary>
Parameters
</summary>
Queryparams Parameters{get;}
}
3. Query page Inherits Iqueryparams interface (querypage.aspx):
/**////<summary>
Querying pages, inheriting interfaces
</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 in the results page
</summary>
Public Queryparams Parameters
{
Get
{
return queryparams;
}
}

private void Btnenter_click (object sender, System.EventArgs e)
{
Assign value
Queryparams = new Queryparams ();
Queryparams.stadate = This.txtStaDate.Text;
Queryparams.enddate = This.txtEndDate.Text

Server.Transfer ("resultpage.aspx");
}
}
4, the other page is also set

Page Pass Value

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.