Monday, August 19, 2013

What is ViewState? In Asp.net

 
 What is ViewState?
This question ask to time of interview or it is comment question for ASP.Net Developer.

ViewState is used to maintain the state of controls during page postback and if we save any control values or anything in viewstate we can access those values throughout the page whenever it required for that
check


The Below i was create one ASPX page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>View State in asp.net Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    ViewState Data:
                </td>
                <td>
                    <b>
                        <asp:Label ID="lblString" runat="server" /></b>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnClick" runat="server" Text="Get ViewState Data" OnClick="btnClick_Click" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>


Know Time to .cs file (add following namespaces in your code behind )
C# Code:-using System;

The full code as below :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdoDotNetExample
{
    public partial class ViewState : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string str = "WELCOME To MR.Arjun Walmiki Blog";
                if (ViewState["Simple Text"] == null)
                {
                ViewState["Simple Text"]=str;
                }
            }

        }

        protected void btnClick_Click(object sender, EventArgs e)
        {
            lblString.Text = ViewState["Simple Text"].ToString();
        }
    }
}
Run The program





if you Click on button  the result are as below :-


ViewState Data: WELCOME To MR.Arjun Walmiki Blog







Hope I help you 

Thank you Visit Here 

Arjun Walmiki

Keep Study see you soon

No comments:

Post a Comment