Connecting Tech Pros Worldwide Help | Site Map

How to retain ViewState while Page Refresh when using UpdatePanel?

Newbie
 
Join Date: Jul 2008
Posts: 16
#1: 4 Weeks Ago
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:
Expand|Select|Wrap|Line Numbers
  1. // Default.aspx:
  2.  
  3. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPCS2008ViewState._Default" 
  4. EnableViewState="true" ViewStateEncryptionMode="Always" %>
  5.  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7.  
  8. <script runat="server">
  9.  
  10.     private int Counter
  11.     {
  12.         get
  13.         {
  14.             object Instance = ViewState["Counter"];
  15.             return (Instance == null) ? 0 : (int)Instance;
  16.         }
  17.         set
  18.         {
  19.             ViewState["Counter"] = value;
  20.         }
  21.     }
  22.  
  23.     protected void Button1_Click(object sender, EventArgs e)
  24.     {
  25.         Counter++;
  26.         TextBox1.Text = Counter.ToString();
  27.     }
  28.  
  29.     protected void Button2_Click(object sender, EventArgs e)
  30.     {
  31.         Response.Redirect("SomePage.aspx");
  32.     }  
  33.  
  34. </script>
  35.  
  36. <html xmlns="http://www.w3.org/1999/xhtml" >
  37. <head runat="server">
  38.     <title></title>
  39. </head>
  40. <body>
  41.     <form id="form1" runat="server" >
  42.     <asp:ScriptManager ID="ScriptManager1" runat="server" />
  43.     <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
  44.     <ContentTemplate>
  45.  
  46.     <div>
  47.  
  48.         <asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" />
  49.         <br />
  50.         <br />
  51.         <span lang="en-us">
  52.         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  53.         <br />
  54.         </span>
  55.         <br />
  56.         <asp:Button ID="Button2" runat="server" Text="Go Some Page" 
  57.             onclick="Button2_Click" />
  58.         <br />
  59.         <br />
  60.  
  61.     </div>
  62.  
  63.      </ContentTemplate>
  64.      </asp:UpdatePanel>
  65.     </form>
  66. </body>
  67. </html>
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. // SomePage.aspx
  76.  
  77. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SomePage.aspx.cs" Inherits="ASPCS2008ViewState.SomePage" 
  78. EnableViewState="true" ViewStateEncryptionMode="Always" %>
  79.  
  80. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  81.  
  82. <script runat="server">
  83.     protected void Button2_Click(object sender, EventArgs e)
  84.     {
  85.         Response.Redirect("Default.aspx");
  86.     }
  87. </script>
  88.  
  89. <html xmlns="http://www.w3.org/1999/xhtml" >
  90. <head runat="server">
  91.     <title></title>
  92. </head>
  93. <body>
  94.     <form id="form1" runat="server">
  95.  
  96.     <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
  97.     <asp:UpdatePanel ID="UpdatePanel1" runat="server" >
  98.     <ContentTemplate>
  99.     <p>
  100.         <span lang="en-us">Wellcome to Some Page! </span>
  101.     </p>
  102.  
  103.     <p>
  104.     <asp:Button ID="Button2" runat="server" Text="Go back" onclick="Button2_Click" />
  105.     </p>
  106.     <div>
  107.  
  108.     </div>
  109.  
  110.     </ContentTemplate>
  111.     </asp:UpdatePanel>
  112.     </form>
  113. </body>
  114. </html>

Thanks
best answer - posted by Frinavale
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
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#2: 3 Weeks Ago

re: How to retain ViewState while Page Refresh when using UpdatePanel?


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
Newbie
 
Join Date: Jul 2008
Posts: 16
#3: 2 Weeks Ago

re: How to retain ViewState while Page Refresh when using UpdatePanel?


Thanks for the reply!
Reply