G
Guest
Hi,
Can anyone tell me why in the code below, the call to ClearChildViewState()
has no effect?
To paraphrase the code: I'm using view state. I have a textbox and a submit
button (and a label that can be ignored). When I press the button the first
time, the click handler hides the textbox. Pressing the button a second time
unhides the textbox. The text box is maintaining its value when hidden via
view state. (The value is NOT being populated due to postback data.) I want a
server-side function to reset a form to it's initial state (i.e. the state it
was in when I first browsed to it). I expected Page.ClearChildViewState() to
do this, but it doesn't. Why not? And what can I do to reset my form on the
server without disabling viewstate?
Thanks,
- Lee
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebTest.WebForm1" EnableViewState="true" %>
<%@ Register TagPrefix="My" Namespace="WebTest" Assembly="WebTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body >
<form id="Form1" method="post" runat="server">
<My:TextBox id="TextBox1" runat="server" EnableViewState="true"></My:TextBox>
<asp:Button id="Button1" runat="server" Text="Hide"></asp:Button>
<p><asp:Label id="Label1" runat="server"></asp:Label></p>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if (!this.IsTrackingViewState)
throw new ApplicationException();
WebTest.TextBox c = (WebTest.TextBox)this.FindControl("TextBox1");
System.Diagnostics.Trace.WriteLine(c.GetViewState().Count);
this.ClearChildViewState(); // Why does this have no effect?
System.Diagnostics.Trace.WriteLine(c.GetViewState().Count);
if (this.Button1.Text == "Hide")
{
this.Button1.Text = "Show";
this.TextBox1.Visible = false;
if (this.TextBox1.Text.Length > 0)
this.Label1.Text = this.TextBox1.Text;
}
else // "Show"
{
this.Button1.Text = "Hide";
this.TextBox1.Visible = true;
}
}
}
}
using System;
using System.Web.UI;
namespace WebTest
{
public class TextBox : System.Web.UI.WebControls.TextBox
{
protected override object SaveViewState()
{
object vs = null;
vs = base.SaveViewState ();
return vs;
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
}
public StateBag GetViewState()
{
return this.ViewState;
}
}
}
Can anyone tell me why in the code below, the call to ClearChildViewState()
has no effect?
To paraphrase the code: I'm using view state. I have a textbox and a submit
button (and a label that can be ignored). When I press the button the first
time, the click handler hides the textbox. Pressing the button a second time
unhides the textbox. The text box is maintaining its value when hidden via
view state. (The value is NOT being populated due to postback data.) I want a
server-side function to reset a form to it's initial state (i.e. the state it
was in when I first browsed to it). I expected Page.ClearChildViewState() to
do this, but it doesn't. Why not? And what can I do to reset my form on the
server without disabling viewstate?
Thanks,
- Lee
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebTest.WebForm1" EnableViewState="true" %>
<%@ Register TagPrefix="My" Namespace="WebTest" Assembly="WebTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body >
<form id="Form1" method="post" runat="server">
<My:TextBox id="TextBox1" runat="server" EnableViewState="true"></My:TextBox>
<asp:Button id="Button1" runat="server" Text="Hide"></asp:Button>
<p><asp:Label id="Label1" runat="server"></asp:Label></p>
</form>
</body>
</HTML>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if (!this.IsTrackingViewState)
throw new ApplicationException();
WebTest.TextBox c = (WebTest.TextBox)this.FindControl("TextBox1");
System.Diagnostics.Trace.WriteLine(c.GetViewState().Count);
this.ClearChildViewState(); // Why does this have no effect?
System.Diagnostics.Trace.WriteLine(c.GetViewState().Count);
if (this.Button1.Text == "Hide")
{
this.Button1.Text = "Show";
this.TextBox1.Visible = false;
if (this.TextBox1.Text.Length > 0)
this.Label1.Text = this.TextBox1.Text;
}
else // "Show"
{
this.Button1.Text = "Hide";
this.TextBox1.Visible = true;
}
}
}
}
using System;
using System.Web.UI;
namespace WebTest
{
public class TextBox : System.Web.UI.WebControls.TextBox
{
protected override object SaveViewState()
{
object vs = null;
vs = base.SaveViewState ();
return vs;
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState (savedState);
}
public StateBag GetViewState()
{
return this.ViewState;
}
}
}