Asp.net recommended method for passing large amounts of data between pages

  • 2020-05-26 08:10:27
  • OfStack

Can you get data to be passed between two different sites? I now want to transfer the data from the A site to the B site...

Cache is recommended
(1) does not affect the performance of the program is not likely, you said, is a large number of data. Let me give you an example. You went from A.aspx to B.aspx. So if you have two users accessing A, do you want your data to not affect different clients? If this is the case, then you can't use Cache for your storage (not absolutely, but you have to differentiate between clients, you have more work to do), only Session, Cookies, ViewState, QueryString, Form, etc.

Condition 2: if you have more than one page to do this, for example, A.aspx and B.aspx both need to pass "a lot of data" to C.aspx, then you can't overwrite each other if you have it in Session. So if it's a small amount of data, like just one number, then ViewState, QueryString, Form will do, but they need an extra trip between the server and the client. With a lot of data like yours, QueryString probably won't work. And ViewState is actually Form. You can consider 1 under the actual situation to choose the specific method.

(2) use the Server.Transfer method
It USES the Server.Transfer method to guide the process from the current page to another page. The new page USES the reply flow from the previous page. The following code shows the method to use when many parameters are required, and it is not necessary to use this method if there are few parameters.

If all the query pages inherit an interface, and a method is defined in the interface, the only function of this method is to let the result page get the parameters needed to build the results, so that multiple pages can share 1 result page operation!
1. Define a class and use it to place all query parameters:
 
/**//// <summary> 
/// QueryParams  Summary of  
/// </summary> 
public class QueryParams 
{ 
private string firstName; 
private string lastname; 
private int age; 
public string Firstname 
{ 
get { return this.firstname; } 
set { this.firstname = value; } 
} 
public string LastName 
{ 
get { return this.lastname; } 
set { this.lastname = value; } 
} 
public string Age 
{ 
get { return this.age; } 
set { this.age = value; } 
} 
} 

2. Interface definition:
 
/**//// <summary > 
///  Define the query interface.  
/// </summary > 
public interface IQueryParams 
{ 
/**//// <summary > 
///  parameter  
/// </summary > 
QueryParams Parameters { get;} 
} 

3. Query page inheritance IQueryParams interface (QueryPage.aspx) :
QueryPage.aspx
 
<form id="form1" runat="server"> 
<div> 
<asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox> 
<asp:TextBox ID="txtLastName" runat="server"></asp:TextBox> 
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox> 
<asp:Button ID="btnEnter" runat="server" Text="Button" OnClick="btnEnter_Click" /></div> 
</form> 

QueryPage.aspx.cs
 
public partial class QueryPage : System.Web.UI.Page, IQueryParams 
{ 
private QueryParams queryParams; 
public QueryParams Parameters 
{ 
get 
{ 
return queryParams; 
} 
} 
public void btnEnter_Click(object sender, System.EventArgs e) 
{ 
// The assignment  
queryParams = new QueryParams(); 
queryParams.FirstnName = this.txtFirstName.Text; 
queryParams.Lastname = this.txtLastName.Text; 
queryParams.Age = this.txtAge.Text; 
Server.Transfer( "ResultPage.aspx "); 
} 
protected void Page_Load(object sender, EventArgs e) 
{ } 
} 

4. Receiving page (ResultPage.aspx) :
ResultPage.aspx.cs
 
public partial class ResultPage : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
QueryParams queryParams = new QueryParams(); 
IQueryParams queryInterface; 
// The page that implements the interface  
if (Context.Handler is IQueryParams) 
{ 
queryInterface = (IQueryParams)Context.Handler; 
queryParams = queryInterface.Parameters; 
} 
Response.Write("FirstName :  "); 
Response.Write(queryParams.FirstName); 
Response.Write(" <br/ >Lastname :  "); 
Response.Write(queryParams.LastName); 
Response.Write(" <br/ >Age :  "); 
Response.Write(queryParams.Age); 
} 
} 

Related articles: