M
multiformity
The following page will not load the view state from the statebag
without throwing an exception. After we get this working, I will be
working on viewstate for some custom controls, but one thing at a time.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Simple test of view state, we are trying to persist the value for i
among multiple postbacks.
/// </summary>
/*
Use the following ASPX page to test this class:
**********************************************
<%@ Page language="c#" AutoEventWireup="false" Inherits="TestPostback"
CodeFile="TestPostback.aspx.cs" EnableViewState="true" %>
<HTML>
<body>
<form runat="server" id="GridForm">
<asplaceHolder ID="_Content" Runat="server" />
<br />
<asp:Literal ID=_Literal runat=server />
</form>
</HTML>
***********************************************
*/
public partial class TestPostback : Page {
private int i;
override protected void OnInit(EventArgs e) {
this.EnableViewState = true;
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
_Content.EnableViewState = true;
}
protected override void LoadViewState(object savedState) {
try {
//This line throws an exception,
//and the viewstate collection is empty
i = (int)ViewState["index"];
_Literal.Text = (string)ViewState["text"];
base.LoadViewState(savedState);
} catch { }
}
protected override object SaveViewState() {
ViewState["index"] = i;
ViewState["text"] = _Literal.Text;
return base.SaveViewState();
}
protected void Page_Load(object sender, System.EventArgs e) {
++i;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Text = "Click";
_Content.Controls.Add(button);
}
void button_Click(object sender, EventArgs e){
//We will note here that the _Literal controls state
//has persisted, but the index has not incremented.
_Literal.Text += i.ToString() + "<br />";
}
}
without throwing an exception. After we get this working, I will be
working on viewstate for some custom controls, but one thing at a time.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// Simple test of view state, we are trying to persist the value for i
among multiple postbacks.
/// </summary>
/*
Use the following ASPX page to test this class:
**********************************************
<%@ Page language="c#" AutoEventWireup="false" Inherits="TestPostback"
CodeFile="TestPostback.aspx.cs" EnableViewState="true" %>
<HTML>
<body>
<form runat="server" id="GridForm">
<asplaceHolder ID="_Content" Runat="server" />
<br />
<asp:Literal ID=_Literal runat=server />
</form>
</HTML>
***********************************************
*/
public partial class TestPostback : Page {
private int i;
override protected void OnInit(EventArgs e) {
this.EnableViewState = true;
this.Load += new EventHandler(Page_Load);
base.OnInit(e);
_Content.EnableViewState = true;
}
protected override void LoadViewState(object savedState) {
try {
//This line throws an exception,
//and the viewstate collection is empty
i = (int)ViewState["index"];
_Literal.Text = (string)ViewState["text"];
base.LoadViewState(savedState);
} catch { }
}
protected override object SaveViewState() {
ViewState["index"] = i;
ViewState["text"] = _Literal.Text;
return base.SaveViewState();
}
protected void Page_Load(object sender, System.EventArgs e) {
++i;
Button button = new Button();
button.Click += new EventHandler(button_Click);
button.Text = "Click";
_Content.Controls.Add(button);
}
void button_Click(object sender, EventArgs e){
//We will note here that the _Literal controls state
//has persisted, but the index has not incremented.
_Literal.Text += i.ToString() + "<br />";
}
}