When we use server. Transfer, we can upload the Request Parameters on the previous page to the next page. But how can we pass viewstate together?
The main method is to first cache viewstate and then obtain it through the cache. The cache can be session.
Someone has written a method on the internet, mainly to return viewstate using the method first. Then, you can obtain it from the reverse interface on the call page. Similar to caching.
Example:
Default. aspx page:
Add a viewstate, add the returnviewstate () method that returns viewsate, and use server. Transfer to redirect,CodeAs follows:
Protected Void Page_load ( Object Sender, eventargs E)
{
If (! Ispostback)
{
Viewstate [ " Vid " ] = " Test " ;
Server. Transfer ( " Tra. aspx " );
}
}
/// <Summary>
/// Return the viewstate of the current page using this method.
/// </Summary>
/// <Returns> </returns>
Public Statebag returnviewstate ()
{
Return Viewstate;
}
}
Tra. ASPX page:
The viewstate of the previous page is obtained through reverse segments on this page.
Protected Void Page_load ( Object Sender, eventargs E)
{
Initialdata ();
}
/// <Summary>
/// Get viewstate through reflection and display
/// </Summary>
Private Void Initialdata ()
{
If ( Null ! = Previouspage)
{
Object objpreviouspage = (object) previouspage;
Methodinfo objmethod = objpreviouspage. GetType (). getmethod ( " Returnviewstate " );
If (Objmethod! = Null )
{
Statebag viewstagebage = (statebag) objmethod. Invoke (objpreviouspage, Null );
If (Viewstagebage! = Null )
{
If ( Null ! = Viewstagebage [ " Vid " ])
{
Response. Write ( " Vid: " + Viewstagebage [ " Vid " ]. Tostring () + " <Br/> " );
}
}
}
}
}