473,320 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:
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
Oct 24 '09 #1

✓ answered 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

2 12181
Frinavale
9,735 Expert Mod 8TB
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
Oct 27 '09 #2
Thanks for the reply!
Nov 3 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: theo | last post by:
Hi... I wish to extract the text content of an Xml file and assign it to DropDownList controls at runtime.I can get the Xml file text content into the DropDownList controls (Ex...if 5 Xml text...
9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
2
by: Prodip Saha | last post by:
I am sure there is an easy fix for this problem. I am reloading the same page at an interval using the meta refresh tag in aspx page. This page has one textbox and one button. The problem is -when...
1
by: batista | last post by:
Hello, I have a web page, which is being refresh after every 30 secs.Now, there is also a datagrid in it, which i bind in the pageload event. Now the problem is when the page is refresh after...
0
by: tynorton | last post by:
Hey, I've been working on this issue for a while now, with no leads or success. The scenario is a homemade modal dialog using UpdatePanels. There a couple ImageButtons inside of usercontrols...
4
by: Dan | last post by:
Hi, i'm not sure to understand the difference between refreshing the pagina by clicking on 'refresh' in the browser and a postback. What i think it is: Suppose a page with a form containing a...
6
by: jojoba | last post by:
hi everyone! i'm serving up an asp.net page using ajax futures. long story short: i have two update panels. one has a webpage in it (e.g. www.google.com). the other has an image inside of a...
0
by: Mattias Sundström | last post by:
Hi! I have an annoying problem concerning viewstate in dynamically loaded user controls. I have a menu and a placeholder on an aspx page. The menu uses AsyncPostbackTriggers to call the...
8
by: Ben | last post by:
Hi, i'm trying to build a simple site navigation (using c#)... i implemented a master page that shows a sidebar user control on the left side, and a content page on the right. i click the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.