Thursday, September 08, 2011

Accessing View State of one page on other ASP.Net Page

Posted by Rahul Kharde at 11:21 PM

There was one question always occurs, can we access viewstate one page to other page? And answer is Yes

“Is it possible to access the ViewState variable of one page on another page?”

We can access the viewstate variables across pages. This is only possible if Cross Page Posting or Server.transfer is used to redirect the user to other page. If Response.redirect is used, then ViewState cannot be accessed across pages.

Here I have two pages Default.aspx and Default2.aspx.I have one Textbox and button control in first page (Default.aspx), after submit button it will be redirect to other page (Default2.aspx) were u get first page viewstate


<html xmlns="">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
    <form id="form2" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" Style="height: 26px" />

Code Behind
protected void Page_Load(object sender, EventArgs e)
public StateBag ReturnViewState()
    return ViewState;
protected void Button1_Click(object sender, EventArgs e)
    ViewState["test"] = TextBox1.Text.ToString();

protected void Page_Load(object sender, EventArgs e)
    StateBag objStateBag = null;
    if (PreviousPage != null)
        Object objPreviousPage = (Object)PreviousPage;
        System.Reflection.MethodInfo objMethod = objPreviousPage.GetType().GetMethod("ReturnViewState");
        objStateBag = (StateBag)objMethod.Invoke(objPreviousPage, null);
    Response.Write("First Page Textbox value : " + objStateBag["test"].ToString());
Suppose we entered “Viewstate testing” in first page text box, 


First Page Textbox value : Viewstate testing
Download code here

If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!

Kindly Bookmark and Share it:



Jagna Co Kalani on November 3, 2017 at 6:43 AM said...

This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.

Have any question? Feel Free To Post Below:


Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top