Thursday 8 August 2013

ViewState

ViewState
http://helpingdotnet.blogspot.in/search/label/View%20State

View State

0 commentsPosted by Rahul Kharde at 3:37 AM

ViewState is used to retain the state of server-side objects between page post backs.

  • ViewState is how ASP.NET Web pages persists data across requests.
  •  ViewState can be encrypted to address security concerns.
  •  ViewState values are accessible with the same page
  •  ViewState is a state management technique build in ASP.NET.
  •  ViewState basically maintains the state of the pages between postbacks.
  •  ViewState maintain the session within the same page.
  •  ViewState allows the state of objects to be stored in a hidden field on the page.

Where the viewstate is stored after the page postback?
Ans: ViewState is stored in a hidden field on the page at client side.  ViewState is transported to the client and back to the server, and is not stored on the server or any other external source.

How long the items in ViewState exists?
Ans: They exist for the life of the current page.

What is the lifespan for items stored in  the ViewState?
Ans: The items stored in ViewState live until the lifetime of the current page expires including postbacks to  same page.

View state is client-side or server side state management techenique?
Ans: View state is client-side state management techenique

How do you enable or disable a ViewState at the page level?
Ans: At the page level you can enable or disable ViewState using EnableViewState property of the page.

What is the name of the hidden form field in which ViewState of the page is saved? 
Ans: __ViewState

Is ViewState of one page available to another page?
Ans: No, ViewState of a Page is available only in that page. You cannot access ViewState of one page from another page.





Read More »

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

0 commentsPosted 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. IfResponse.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

Default.aspx


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"> </asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit"Style="height26px" />
    </div>
    </form>
</body>
</html>

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();
    Server.Transfer("Default2.aspx");
}

Defualt2.aspx
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, 

Output

First Page Textbox value : Viewstate testing
Download code here

No comments:

Post a Comment