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?
Code Snippet:
// 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"> 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>
<div>
<asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Button_Click"></asp:Label>
<span lang="en-us">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</span>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="IsPostBack"></asp:Label>
<span lang="en-us">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</span>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="NotIsPostBack"></asp:Label>
<span lang="en-us">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<br />
</span>
<asp:Label ID="Label4" runat="server" Text="NotIsCallback"></asp:Label>
<span lang="en-us"> </span>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button2" runat="server" Text="Go Some Page"
onclick="Button2_Click" />
<br />
<br />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
<html
// Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPCS2008ViewState
{
public partial class _Default : System.Web.UI.Page
{
private int Counter
{
get
{
object Instance = ViewState["Counter"];
return (Instance == null) ? 0 : (int)Instance;
}
set
{
ViewState["Counter"] = value;
}
}
private int IsPostBackCounter
{
get
{
object Instance = ViewState["IsPostBackCounter"];
return (Instance == null) ? 0 : (int)Instance;
}
set
{
ViewState["IsPostBackCounter"] = value;
}
}
private int NotIsPostBackCounter
{
get
{
object Instance = ViewState["NotIsPostBackCounter"];
return (Instance == null) ? 0 : (int)Instance;
}
set
{
ViewState["NotIsPostBackCounter"] = value;
}
}
private int NotIsCallbackCounter
{
get
{
object Instance = ViewState["NotIsCallbackCounter"];
return (Instance == null) ? 0 : (int)Instance;
}
set
{
ViewState["NotIsCallbackCounter"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
IsPostBackCounter++;
TextBox2.Text = IsPostBackCounter.ToString();
if (!IsPostBack)
{
NotIsPostBackCounter++;
TextBox3.Text = NotIsPostBackCounter.ToString();
}
if (!IsCallback)
{
NotIsCallbackCounter++;
TextBox4.Text = NotIsCallbackCounter.ToString();
}
}
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");
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
{
Page.RegisterRequiresViewStateEncryption();
}
}
protected override object SaveViewState()
{
object obj = base.SaveViewState();
if (Page != null)
{
if (Page.ViewStateEncryptionMode == ViewStateEncryptionMode.Never)
{
throw new Exception("ViewStateEncryptionMode.Never not allowed when using the SensitiveDataList control.");
}
}
return obj;
}
}
}
// 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"> 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>
<br />
<asp:Button ID="Button1" runat="server" Text="Click" onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Button_Click"></asp:Label>
<span lang="en-us">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</span>
<br />
<br />
<p>
<asp:Button ID="Button2" runat="server" Text="Go back" onclick="Button2_Click" />
</p>
<div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
<html
// SomePage.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ASPCS2008ViewState
{
public partial class SomePage : System.Web.UI.Page
{
private int Counter
{
get
{
object Instance = ViewState["Counter"];
return (Instance == null) ? 0 : (int)Instance;
}
set
{
ViewState["Counter"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Counter++;
TextBox1.Text = Counter.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
{
Page.RegisterRequiresViewStateEncryption();
}
}
}
}
Please help!
Thanks.