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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...