Tuesday, September 07, 2010

LoadViewState(...) method does not fire in a postback

Unless you have some values in the ViewState the LoadViewState(...) method will not get fired in a postback.
Example:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script runat="server">
        public string Value
        {
            get { return (string)ViewState["Value"]; }
            set { ViewState["Value"] = value; }
        }
        protected override void LoadViewState(object savedState)
        {
            Response.Write("Load View State <br />");
            base.LoadViewState(savedState);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Response.Write("Page Load <br />");
        }
        protected void Save(object sender, EventArgs e)
        {
            Response.Write("My Save Method - I didnt add anyting to View state <br />");
        }
        protected override object SaveViewState()
        {
            Response.Write("Save View State <br />");
            return base.SaveViewState();
        }
        protected void Add(object sender, EventArgs e)
        {
            Response.Write(string.Format("My Add Method - I add some value to view state <strong>{0}</strong> <br />",
                string.IsNullOrEmpty(this.Value) ? "Next time page will load view sate" : string.Empty));
            this.Value = DateTime.Now.ToShortTimeString();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="ProductList">
            <asp:Button runat="server" ID="btnSave" OnClick="Save" Text="Save" />
            <asp:Button runat="server" ID="btnAdd" OnClick="Add" Text="Add" />
        </div>
    </div>
    </form>
</body>
</html>

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...