How to retain ViewState while Page Refresh when using UpdatePanel?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qwedster
    New Member
    • Jul 2008
    • 24

    How to retain ViewState while Page Refresh when using UpdatePanel?

    Folks!

    The following is a "Hello World" kind of code for ViewState. I just want to know how to retain the ViewState 1) while Page Refresh when using UpdatePanel and also 2) While I reverting back to the page after round trip when using UpdatePanel?

    In the following code snippet the ViewState is killed when I click page refresh or when I go some page and revert back

    Code Snippet:
    Code:
    // Default.aspx:
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPCS2008ViewState._Default" 
    EnableViewState="true" ViewStateEncryptionMode="Always" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
    
        private int Counter
        {
            get
            {
                object Instance = ViewState["Counter"];
                return (Instance == null) ? 0 : (int)Instance;
            }
            set
            {
                ViewState["Counter"] = value;
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            Counter++;
            TextBox1.Text = Counter.ToString();
        }
    
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("SomePage.aspx");
        }  
        
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server" >
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
        <ContentTemplate>
       
        <div>
        
            <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" />
            <br />
            <br />
            <span lang="en-us">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            </span>
            <br />
            <asp:Button ID="Button2" runat="server" Text="Go Some Page" 
                onclick="Button2_Click" />
            <br />
            <br />
        
        </div>
        
         </ContentTemplate>
         </asp:UpdatePanel>
        </form>
    </body>
    </html>
    
    
    
    
    
    
    
    // SomePage.aspx
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SomePage.aspx.cs" Inherits="ASPCS2008ViewState.SomePage" 
    EnableViewState="true" ViewStateEncryptionMode="Always" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx");
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        
        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
        <ContentTemplate>
        <p>
            <span lang="en-us">Wellcome to Some Page! </span>
        </p>
        
        <p>
        <asp:Button ID="Button2" runat="server" Text="Go back" onclick="Button2_Click" />
        </p>
        <div>
        
        </div>
        
        </ContentTemplate>
        </asp:UpdatePanel>
        </form>
    </body>
    </html>

    Thanks
    Last edited by Frinavale; Oct 27 '09, 01:32 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The Refresh button?
    Like the browser refresh button? Or is one of your buttons a "refresh button".

    If you're talking about the web-browser's refresh button then this is expected behaviour....


    The web browser makes a request for the page
    The initial web page is sent to the browser
    You do a bunch of asynchronous requests to the server and your counter is changed
    Now you hit the refresh button....the refresh button preforms the last "full page request" to the server which means that it loads the initial page again.

    -Frinny

    Comment

    • qwedster
      New Member
      • Jul 2008
      • 24

      #3
      Thanks for the reply!

      Comment

      Working...