Passing viewstate method in server. Transfer

Source: Internet
Author: User
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/> " );
}
}
}
}
}

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.