3
Reply

can anyone give me what is view state with example?

    HTTP is a stateless protocol. So It can't  hold the state of  the control between the postback. Viewstate means storing the state of the server control between the page postback. We can Enable and Disable this property in the each control by 'EnableViewState' property. It stores in the hidden filed by the form of Base64 Encoded string.

    Example :  We can see Type,Name and Base64 Encoded string

    <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJODczNjQ5OTk0D2QWAgIDD2QWAgIFDw8WAh4EV
    GV4dAUWSSBMb3ZlIERvdG5ldEN1cnJ5LmNvbWRkZMHbBY9JqBTvB5
    /6kXnY15AUSAwa" />
     
    We can Decode it easily:
    byte[] decode = Convert.FromBase64String(txtViewState.Text);
            txtViewState.Text = System.Text.Encoding.ASCII.GetString(decode);

    View State is basically a client side trechnique,

    it allows the state of object to be stored ion a hidden field. on the page,

    its lifespan :- life of the current page (including postback)

    [email protected]

    Each control on a Web Forms page, including the page itself, has a ViewState property, it is a built-in struture for automatic retention of page and control state, which means you don’t need to do anything about getting back the data of controls after posting page to the server.

    Here, which is useful to us is the ViewState property, we can use it to save information between round trips to the server.
    [c#]
    //to save information
    ViewState.Add(“shape”,”circle”);
    //to retrieve information
    string shapes=ViewState[“shape”];

    Note: Unlike Hidden Field, the values in ViewState are invisible when ‘view source’, they are compressed and encoded.

    Regards

    Sekhar Reddy.B