473,395 Members | 1,977 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,395 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 12199
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.