473,721 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to perform server-side form reset?


Hi,

Can anyone tell me why in the code below, the call to ClearChildViewS tate()
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.ClearChild ViewState() 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="Web Form1.aspx.cs" AutoEventWireup ="false"
Inherits="WebTe st.WebForm1" EnableViewState ="true" %>
<%@ Register TagPrefix="My" Namespace="WebT est" Assembly="WebTe st" %>
<!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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace WebTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Text Box TextBox1;

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
if (!this.IsTracki ngViewState)
throw new ApplicationExce ption();

WebTest.TextBox c = (WebTest.TextBo x)this.FindCont rol("TextBox1") ;

System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
this.ClearChild ViewState(); // Why does this have no effect?
System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);

if (this.Button1.T ext == "Hide")
{
this.Button1.Te xt = "Show";
this.TextBox1.V isible = false;

if (this.TextBox1. Text.Length > 0)
this.Label1.Tex t = this.TextBox1.T ext;
}
else // "Show"
{
this.Button1.Te xt = "Hide";
this.TextBox1.V isible = true;
}
}
}
}

using System;
using System.Web.UI;

namespace WebTest
{
public class TextBox : System.Web.UI.W ebControls.Text Box
{
protected override object SaveViewState()
{
object vs = null;

vs = base.SaveViewSt ate ();

return vs;
}

protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate (savedState);
}

public StateBag GetViewState()
{
return this.ViewState;
}
}
}
Nov 19 '05 #1
4 5324
because the click event fires long after the textbox has feteched its value
from viewstate.

-- bruce (sqlwork.com)
"Lee Chapman" <Le********@new sgroup.nospam> wrote in message
news:B8******** *************** ***********@mic rosoft.com...

Hi,

Can anyone tell me why in the code below, the call to
ClearChildViewS tate()
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.ClearChild ViewState()
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="Web Form1.aspx.cs"
AutoEventWireup ="false"
Inherits="WebTe st.WebForm1" EnableViewState ="true" %>
<%@ Register TagPrefix="My" Namespace="WebT est" Assembly="WebTe st" %>
<!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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace WebTest
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Butt on Button1;
protected System.Web.UI.W ebControls.Labe l Label1;
protected System.Web.UI.W ebControls.Text Box TextBox1;

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent()
{
this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
}
#endregion

private void Button1_Click(o bject sender, System.EventArg s e)
{
if (!this.IsTracki ngViewState)
throw new ApplicationExce ption();

WebTest.TextBox c = (WebTest.TextBo x)this.FindCont rol("TextBox1") ;

System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
this.ClearChild ViewState(); // Why does this have no effect?
System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);

if (this.Button1.T ext == "Hide")
{
this.Button1.Te xt = "Show";
this.TextBox1.V isible = false;

if (this.TextBox1. Text.Length > 0)
this.Label1.Tex t = this.TextBox1.T ext;
}
else // "Show"
{
this.Button1.Te xt = "Hide";
this.TextBox1.V isible = true;
}
}
}
}

using System;
using System.Web.UI;

namespace WebTest
{
public class TextBox : System.Web.UI.W ebControls.Text Box
{
protected override object SaveViewState()
{
object vs = null;

vs = base.SaveViewSt ate ();

return vs;
}

protected override void LoadViewState(o bject savedState)
{
base.LoadViewSt ate (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.c om>
| References: <B8************ *************** *******@microso ft.com>
| 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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: rdcsd1.safeco.c om 12.144.134.2
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP10.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1199 90
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| because the click event fires long after the textbox has feteched its
value
| from viewstate.
|
| -- bruce (sqlwork.com)
|
|
| "Lee Chapman" <Le********@new sgroup.nospam> wrote in message
| news:B8******** *************** ***********@mic rosoft.com...
| >
| > Hi,
| >
| > Can anyone tell me why in the code below, the call to
| > ClearChildViewS tate()
| > 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.ClearChild ViewState()
| > 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="Web Form1.aspx.cs"
| > AutoEventWireup ="false"
| > Inherits="WebTe st.WebForm1" EnableViewState ="true" %>
| > <%@ Register TagPrefix="My" Namespace="WebT est" Assembly="WebTe st" %>
| > <!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.Collecti ons;
| > using System.Componen tModel;
| > using System.Data;
| > using System.Drawing;
| > using System.Web;
| > using System.Web.Sess ionState;
| > using System.Web.UI;
| > using System.Web.UI.W ebControls;
| > using System.Web.UI.H tmlControls;
| >
| > namespace WebTest
| > {
| > /// <summary>
| > /// Summary description for WebForm1.
| > /// </summary>
| > public class WebForm1 : System.Web.UI.P age
| > {
| > protected System.Web.UI.W ebControls.Butt on Button1;
| > protected System.Web.UI.W ebControls.Labe l Label1;
| > protected System.Web.UI.W ebControls.Text Box TextBox1;
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArg s e)
| > {
| > InitializeCompo nent();
| > base.OnInit(e);
| > }
| >
| > private void InitializeCompo nent()
| > {
| > this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
| > }
| > #endregion
| >
| > private void Button1_Click(o bject sender, System.EventArg s e)
| > {
| > if (!this.IsTracki ngViewState)
| > throw new ApplicationExce ption();
| >
| > WebTest.TextBox c = (WebTest.TextBo x)this.FindCont rol("TextBox1") ;
| >
| > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| > this.ClearChild ViewState(); // Why does this have no effect?
| > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| >
| > if (this.Button1.T ext == "Hide")
| > {
| > this.Button1.Te xt = "Show";
| > this.TextBox1.V isible = false;
| >
| > if (this.TextBox1. Text.Length > 0)
| > this.Label1.Tex t = this.TextBox1.T ext;
| > }
| > else // "Show"
| > {
| > this.Button1.Te xt = "Hide";
| > this.TextBox1.V isible = true;
| > }
| > }
| > }
| > }
| >
| > using System;
| > using System.Web.UI;
| >
| > namespace WebTest
| > {
| > public class TextBox : System.Web.UI.W ebControls.Text Box
| > {
| > protected override object SaveViewState()
| > {
| > object vs = null;
| >
| > vs = base.SaveViewSt ate ();
| >
| > return vs;
| > }
| >
| > protected override void LoadViewState(o bject savedState)
| > {
| > base.LoadViewSt ate (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)(ViewSt ate["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(o bject savedState)
{
this.action = (string)savedSt ate;
}

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.c om>
| References: <B8************ *************** *******@microso ft.com>
| 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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: rdcsd1.safeco.c om 12.144.134.2
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP10.phx. gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1199 90
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
|
| because the click event fires long after the textbox has feteched its
value
| from viewstate.
|
| -- bruce (sqlwork.com)
|
|
| "Lee Chapman" <Le********@new sgroup.nospam> wrote in message
| news:B8******** *************** ***********@mic rosoft.com...
| >
| > Hi,
| >
| > Can anyone tell me why in the code below, the call to
| > ClearChildViewS tate()
| > 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.ClearChild ViewState()
| > 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="Web Form1.aspx.cs"
| > AutoEventWireup ="false"
| > Inherits="WebTe st.WebForm1" EnableViewState ="true" %>
| > <%@ Register TagPrefix="My" Namespace="WebT est" Assembly="WebTe st" %>
| > <!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.Collecti ons;
| > using System.Componen tModel;
| > using System.Data;
| > using System.Drawing;
| > using System.Web;
| > using System.Web.Sess ionState;
| > using System.Web.UI;
| > using System.Web.UI.W ebControls;
| > using System.Web.UI.H tmlControls;
| >
| > namespace WebTest
| > {
| > /// <summary>
| > /// Summary description for WebForm1.
| > /// </summary>
| > public class WebForm1 : System.Web.UI.P age
| > {
| > protected System.Web.UI.W ebControls.Butt on Button1;
| > protected System.Web.UI.W ebControls.Labe l Label1;
| > protected System.Web.UI.W ebControls.Text Box TextBox1;
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArg s e)
| > {
| > InitializeCompo nent();
| > base.OnInit(e);
| > }
| >
| > private void InitializeCompo nent()
| > {
| > this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
| > }
| > #endregion
| >
| > private void Button1_Click(o bject sender, System.EventArg s e)
| > {
| > if (!this.IsTracki ngViewState)
| > throw new ApplicationExce ption();
| >
| > WebTest.TextBox c = (WebTest.TextBo x)this.FindCont rol("TextBox1") ;
| >
| > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| > this.ClearChild ViewState(); // Why does this have no effect?
| > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| >
| > if (this.Button1.T ext == "Hide")
| > {
| > this.Button1.Te xt = "Show";
| > this.TextBox1.V isible = false;
| >
| > if (this.TextBox1. Text.Length > 0)
| > this.Label1.Tex t = this.TextBox1.T ext;
| > }
| > else // "Show"
| > {
| > this.Button1.Te xt = "Hide";
| > this.TextBox1.V isible = true;
| > }
| > }
| > }
| > }
| >
| > using System;
| > using System.Web.UI;
| >
| > namespace WebTest
| > {
| > public class TextBox : System.Web.UI.W ebControls.Text Box
| > {
| > protected override object SaveViewState()
| > {
| > object vs = null;
| >
| > vs = base.SaveViewSt ate ();
| >
| > return vs;
| > }
| >
| > protected override void LoadViewState(o bject savedState)
| > {
| > base.LoadViewSt ate (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?TGVlIENoYXB tYW4=?=" <Le********@new sgroup.nospam>
| References: <B8************ *************** *******@microso ft.com>
<#J************ **@TK2MSFTNGP10 .phx.gbl>
<KE************ **@TK2MSFTNGXA0 1.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************ *************** *******@microso ft.com>
| 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.publi c.dotnet.framew ork.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.p hx.gbl 10.40.2.250
| Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GXA03.phx.gbl
| Xref: TK2MSFTNGXA01.p hx.gbl
microsoft.publi c.dotnet.framew ork.aspnet:1202 05
| X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.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)(ViewSt ate["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(o bject savedState)
| {
| this.action = (string)savedSt ate;
| }
|
| 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.c om>
| > | References: <B8************ *************** *******@microso ft.com>
| > | 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.publi c.dotnet.framew ork.aspnet
| > | NNTP-Posting-Host: rdcsd1.safeco.c om 12.144.134.2
| > | Path: TK2MSFTNGXA01.p hx.gbl!TK2MSFTN GP08.phx.gbl!TK 2MSFTNGP10.phx. gbl
| > | Xref: TK2MSFTNGXA01.p hx.gbl
| > microsoft.publi c.dotnet.framew ork.aspnet:1199 90
| > | X-Tomcat-NG: microsoft.publi c.dotnet.framew ork.aspnet
| > |
| > | because the click event fires long after the textbox has feteched its
| > value
| > | from viewstate.
| > |
| > | -- bruce (sqlwork.com)
| > |
| > |
| > | "Lee Chapman" <Le********@new sgroup.nospam> wrote in message
| > | news:B8******** *************** ***********@mic rosoft.com...
| > | >
| > | > Hi,
| > | >
| > | > Can anyone tell me why in the code below, the call to
| > | > ClearChildViewS tate()
| > | > 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.ClearChild ViewState()
| > | > 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="Web Form1.aspx.cs"
| > | > AutoEventWireup ="false"
| > | > Inherits="WebTe st.WebForm1" EnableViewState ="true" %>
| > | > <%@ Register TagPrefix="My" Namespace="WebT est" Assembly="WebTe st"
%>
| > | > <!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.Collecti ons;
| > | > using System.Componen tModel;
| > | > using System.Data;
| > | > using System.Drawing;
| > | > using System.Web;
| > | > using System.Web.Sess ionState;
| > | > using System.Web.UI;
| > | > using System.Web.UI.W ebControls;
| > | > using System.Web.UI.H tmlControls;
| > | >
| > | > namespace WebTest
| > | > {
| > | > /// <summary>
| > | > /// Summary description for WebForm1.
| > | > /// </summary>
| > | > public class WebForm1 : System.Web.UI.P age
| > | > {
| > | > protected System.Web.UI.W ebControls.Butt on Button1;
| > | > protected System.Web.UI.W ebControls.Labe l Label1;
| > | > protected System.Web.UI.W ebControls.Text Box TextBox1;
| > | >
| > | > #region Web Form Designer generated code
| > | > override protected void OnInit(EventArg s e)
| > | > {
| > | > InitializeCompo nent();
| > | > base.OnInit(e);
| > | > }
| > | >
| > | > private void InitializeCompo nent()
| > | > {
| > | > this.Button1.Cl ick += new System.EventHan dler(this.Butto n1_Click);
| > | > }
| > | > #endregion
| > | >
| > | > private void Button1_Click(o bject sender, System.EventArg s e)
| > | > {
| > | > if (!this.IsTracki ngViewState)
| > | > throw new ApplicationExce ption();
| > | >
| > | > WebTest.TextBox c = (WebTest.TextBo x)this.FindCont rol("TextBox1") ;
| > | >
| > | > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| > | > this.ClearChild ViewState(); // Why does this have no effect?
| > | > System.Diagnost ics.Trace.Write Line(c.GetViewS tate().Count);
| > | >
| > | > if (this.Button1.T ext == "Hide")
| > | > {
| > | > this.Button1.Te xt = "Show";
| > | > this.TextBox1.V isible = false;
| > | >
| > | > if (this.TextBox1. Text.Length > 0)
| > | > this.Label1.Tex t = this.TextBox1.T ext;
| > | > }
| > | > else // "Show"
| > | > {
| > | > this.Button1.Te xt = "Hide";
| > | > this.TextBox1.V isible = true;
| > | > }
| > | > }
| > | > }
| > | > }
| > | >
| > | > using System;
| > | > using System.Web.UI;
| > | >
| > | > namespace WebTest
| > | > {
| > | > public class TextBox : System.Web.UI.W ebControls.Text Box
| > | > {
| > | > protected override object SaveViewState()
| > | > {
| > | > object vs = null;
| > | >
| > | > vs = base.SaveViewSt ate ();
| > | >
| > | > return vs;
| > | > }
| > | >
| > | > protected override void LoadViewState(o bject savedState)
| > | > {
| > | > base.LoadViewSt ate (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
16783
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 table X delete corresponding child records in table Y. Table X ========= X_ID SOME_VAL
1
3799
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 another server and then restore onto a database and leave the database in read-only mode for additional transaction logs restore during the day.
8
7294
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 the local C:\test, in this directory I have index.htm and test.asp (just a simple Response.Write "TEST"). Opening the server/test/index.htm show the page, server/test/test.asp gives
1
4913
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 images, jpg images) to Microsoft SQL Server 2000 using Microsoft DTS. On trying to perform this simple conversion I recieved the error "Need to run the object to perform the operation - Exception Access Violation" from Microsoft DTS and my table...
29
3700
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 from buying mdb backends. Here's the (million dollar?) questions :) How long and how difficult a process would it be? Which SQL platform would we be best to develop it on?
3
1855
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 Index.html. After finishing its work, it should spawn a new browser and display the updated Index.html file. How can I do that? Thank you for any response.
14
3887
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 maxRequestLength parameter of the <httpRuntime> section and that works as expected. What I want is to enforce a max file size but haven't been able to trap the error thrown when the file is too large and that's where I could use some help.
2
1845
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 to aspx C. But I face a problem that when I click the button in aspx B, exception appear which said the view state is invalid. But if I have not used Server.Transfer from aspx A to aspx B , the Server.Transfer from aspx B to aspx C works...
2
4922
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 being updated by some other process through remoting. When the page loads, I init the tree, and in my browser I can see the initialized tree. The problem is that every time that I receive update to tree from the remote process,
3
1567
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 would just like to have the SQL backend. Is there anything wrong with moving the tables to SQL and simply linking to them in Access? Thanks
0
8838
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8728
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9059
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4484
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4751
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3188
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.