Monday, 22 July 2013

Get Previous page Viewstate from this Page








Page1.aspx :
void btn_Click(object sender, EventArgs e)
{
  ViewState["Test"] = "Ajay";
  Server.Transfer("Page2.aspx");           
}
public string ReturnViewState()
{
  return ViewState["Test"].ToString();
}


Page2.aspx :

protected void Page_Load(object sender, EventArgs e)
{
  if (PreviousPage != null && PreviousPageViewState != null)
  {
    string str = PreviousPageViewState; //value will bw Ajay
  }
}
private string PreviousPageViewState
{
  get
  {
    string returnValue = null;
    if (PreviousPage != null)
    {
      Object objPreviousPage = (Object)PreviousPage;
      MethodInfo objMethod = objPreviousPage.GetType().GetMethod("ReturnViewState");
      //System.Reflection class
      return (String)objMethod.Invoke(objPreviousPage, null);
    }
    return returnValue;
  }
}

No comments:

Post a comment