473,387 Members | 1,493 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,387 software developers and data experts.

How to perform server-side form reset?


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;
}
}
}
Nov 19 '05 #1
4 5303
because the click event fires long after the textbox has feteched its value
from viewstate.

-- bruce (sqlwork.com)
"Lee Chapman" <Le********@newsgroup.nospam> wrote in message
news:B8**********************************@microsof t.com...

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;
}
}
}

Nov 19 '05 #2
Thanks for Bruce's informative inputs,

Hi Lee,

As Bruce has mentioned, the LoadViewState is getting executed before Load
event for controls/page , after that the propertes value is assigned to the
Control/page instance, and these properteis will be persisted into
viewstate again before Rendering. So change the ViewState in PostBack event
handler ( After Load ViewSate before save viewstate) won't have any effect.
also, IMO, I'd recommend you directly clear those entry fields control's
content through the instance properties rather than accessing the ViewState
collection.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Bruce Barker" <br******************@safeco.com>
| References: <B8**********************************@microsoft.co m>
| Subject: Re: How to perform server-side form reset?
| Date: Wed, 24 Aug 2005 09:01:14 -0700
| Lines: 148
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#J**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: rdcsd1.safeco.com 12.144.134.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:119990
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| because the click event fires long after the textbox has feteched its
value
| from viewstate.
|
| -- bruce (sqlwork.com)
|
|
| "Lee Chapman" <Le********@newsgroup.nospam> wrote in message
| news:B8**********************************@microsof t.com...
| >
| > 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;
| > }
| > }
| > }
| >
| >
|
|
|

Nov 19 '05 #3

Hi,

I see. The examples in ASP.NET Developing Server Controls & Components
implement properties that persist via view state in the following way:

public string Action
{
get
{
string action = (string)(ViewState["Action"]);
return action == null ? String.Empty : action;
}
set
{
ViewState["Action"] = value;
}
}

So I assumed that I would be able to reset the state that a control persists
via view state by clearing view state, even in a click event (which I
understand fires after view state has been restored).

However, I guess most controls must do things differently; perhaps they do
something like:

private string action;

public string Action
{
get { return this.action; }
set { this.action = value; }
}

protected override object SaveViewState()
{
return this.action;
}

protected override void LoadViewState(object savedState)
{
this.action = (string)savedState;
}

Which would explain what's going on.

So going back to my original problem, it looks like I will have to reset my
form manually, as you suggest, by resetting the object properties that I care
about. That's more code - and code that has to remain in sync with the
controls on the page - but if there's no "reset my controls" silver bullet,
then I guess I have no choice.

Thanks,
- Lee
"Steven Cheng[MSFT]" wrote:
Thanks for Bruce's informative inputs,

Hi Lee,

As Bruce has mentioned, the LoadViewState is getting executed before Load
event for controls/page , after that the propertes value is assigned to the
Control/page instance, and these properteis will be persisted into
viewstate again before Rendering. So change the ViewState in PostBack event
handler ( After Load ViewSate before save viewstate) won't have any effect.
also, IMO, I'd recommend you directly clear those entry fields control's
content through the instance properties rather than accessing the ViewState
collection.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Bruce Barker" <br******************@safeco.com>
| References: <B8**********************************@microsoft.co m>
| Subject: Re: How to perform server-side form reset?
| Date: Wed, 24 Aug 2005 09:01:14 -0700
| Lines: 148
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| Message-ID: <#J**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: rdcsd1.safeco.com 12.144.134.2
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:119990
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| because the click event fires long after the textbox has feteched its
value
| from viewstate.
|
| -- bruce (sqlwork.com)
|
|
| "Lee Chapman" <Le********@newsgroup.nospam> wrote in message
| news:B8**********************************@microsof t.com...
| >
| > 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;
| > }
| > }
| > }
| >
| >
|
|
|

Nov 19 '05 #4
Thanks for the followup Lee,

Yes, manually clear all the entry fields controls may cause the page code
become tight-coupled, but it'll be the most efficient way. Also, if you
want to make it more flexible, we can use the FindControl method to find
all the enty fields controls thorugh the control ID and reset their text
property, but this will be much less efficient.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Thread-Topic: How to perform server-side form reset?
| thread-index: AcWpW6aq6sDuDiD+SgiDrP/xnnn3Lw==
| X-WBNR-Posting-Host: 194.200.242.250
| From: "=?Utf-8?B?TGVlIENoYXBtYW4=?=" <Le********@newsgroup.nospam>
| References: <B8**********************************@microsoft.co m>
<#J**************@TK2MSFTNGP10.phx.gbl>
<KE**************@TK2MSFTNGXA01.phx.gbl>
| Subject: Re: How to perform server-side form reset?
| Date: Thu, 25 Aug 2005 02:59:15 -0700
| Lines: 253
| Message-ID: <2F**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:120205
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
|
| Hi,
|
| I see. The examples in ASP.NET Developing Server Controls & Components
| implement properties that persist via view state in the following way:
|
| public string Action
| {
| get
| {
| string action = (string)(ViewState["Action"]);
| return action == null ? String.Empty : action;
| }
| set
| {
| ViewState["Action"] = value;
| }
| }
|
| So I assumed that I would be able to reset the state that a control
persists
| via view state by clearing view state, even in a click event (which I
| understand fires after view state has been restored).
|
| However, I guess most controls must do things differently; perhaps they
do
| something like:
|
| private string action;
|
| public string Action
| {
| get { return this.action; }
| set { this.action = value; }
| }
|
| protected override object SaveViewState()
| {
| return this.action;
| }
|
| protected override void LoadViewState(object savedState)
| {
| this.action = (string)savedState;
| }
|
| Which would explain what's going on.
|
| So going back to my original problem, it looks like I will have to reset
my
| form manually, as you suggest, by resetting the object properties that I
care
| about. That's more code - and code that has to remain in sync with the
| controls on the page - but if there's no "reset my controls" silver
bullet,
| then I guess I have no choice.
|
| Thanks,
| - Lee
|
|
| "Steven Cheng[MSFT]" wrote:
|
| > Thanks for Bruce's informative inputs,
| >
| > Hi Lee,
| >
| > As Bruce has mentioned, the LoadViewState is getting executed before
Load
| > event for controls/page , after that the propertes value is assigned to
the
| > Control/page instance, and these properteis will be persisted into
| > viewstate again before Rendering. So change the ViewState in PostBack
event
| > handler ( After Load ViewSate before save viewstate) won't have any
effect.
| > also, IMO, I'd recommend you directly clear those entry fields
control's
| > content through the instance properties rather than accessing the
ViewState
| > collection.
| >
| > Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | From: "Bruce Barker" <br******************@safeco.com>
| > | References: <B8**********************************@microsoft.co m>
| > | Subject: Re: How to perform server-side form reset?
| > | Date: Wed, 24 Aug 2005 09:01:14 -0700
| > | Lines: 148
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | Message-ID: <#J**************@TK2MSFTNGP10.phx.gbl>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: rdcsd1.safeco.com 12.144.134.2
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP10.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:119990
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | because the click event fires long after the textbox has feteched its
| > value
| > | from viewstate.
| > |
| > | -- bruce (sqlwork.com)
| > |
| > |
| > | "Lee Chapman" <Le********@newsgroup.nospam> wrote in message
| > | news:B8**********************************@microsof t.com...
| > | >
| > | > 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;
| > | > }
| > | > }
| > | > }
| > | >
| > | >
| > |
| > |
| > |
| >
| >
|

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Aidan Whitehall | last post by:
Have gone through BOL and Google, but can't find the answer... please help with a simple Q. I'm trying to create a simple cascade delete trigger in SQL Server 7 where deleting "parent" records in...
1
by: xo55ox | last post by:
Hi, Did anyone successfully set up a local package to first ftp a db.bak and second perform an automated db restore? I need to perform an automated task, which ftp nightly backup file to...
8
by: Rene | last post by:
Hi, I'm spend many hour to fix this problem, read many articles about it but no article gave a solution. To isolate the problem I've created in IIS6 (WServer2003) a virtual directory test to...
1
by: gary.scott | last post by:
Aaaaaarrgghh ! (that's better) I am trying to convert a field within my Oracle 9i Database that is of type BLOB (but this BLOB may contain a combination of clobs/varchars or images such as gif...
29
by: Mark B | last post by:
We have an Access app (quite big) at www.orbisoft.com/download. We have had requests by potential users to have it converted to an SQL version for them since there corporate policy excludes them...
3
by: Teckie03 | last post by:
Hi, does anyone know how to display a seperate browser from server process? My ASP.NET app control (ascx) has Archive button that does its own work , including updating an html page called...
14
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the...
2
by: Tony Cheng | last post by:
When I use Server.Transfer from aspx A to aspx B, it's ok but when I click a button in aspx B, the expected behaviour is that Server.Transfer would be called again and the page would go from aspx B...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
3
by: AP | last post by:
Hello I have a department full of Access databases. I am starting to think about moving some of the larger ones to use a sql server backend. I do not want to go throught the technical upsizing. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.