Home > Archive > ASP .NET Webcontrols > January 2006 > EnableViewState=False creates problems with Visible & Enabled
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
EnableViewState=False creates problems with Visible & Enabled
|
|
| Revoemag 2006-01-31, 7:08 pm |
| I have a very simple WebForm that has 3 Web Controls: 1 TextBox (TextBox1)
and 2 Buttons (Button1 & Button2).
Button1 checks if the TextBox is Visible (or Enabled) and sets it to False
(or True in the opposite case). Button2 does absolutely nothing.
If I set EnableViewState=False on the TextBox, I am no longer able to
control the .Visible or .Enabled properties. Button1 will set .Visible=False,
but will never set it to True again. Button2 (which does nothing) will bring
it back up (eg set it back to it's original state).
With EnableViewState=True, it works as expected. Button1 toggles TextBox1,
Button2 does nothing.
------------ Code --------------
<script runat="Server">
Sub Button1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs)
TextBox1.Visible = Not (TextBox1.Visible)
End Sub
Sub Button2_Click(ByVal sender As System.Object, ByVal e as System.EventArgs)
' Do nothing
End Sub
</script>
<html>
<head><title>Form Test</title></head>
<body>
<form Runat="Server">
<asp:TextBox
ID="TextBox1"
EnableViewState="False"
Runat="Server" /><br />
<asp:Button
ID="Button1" Text="Submit"
OnClick="Button1_Click"
Runat="Server" />
<asp:Button
ID="Button2"
Text="Do Nothing"
OnClick="Button2_Click"
Runat="Server" />
</form>
</body>
</html>
------------ Code --------------
This is a small example the issue that I use in a much more complex state. I
can write code to work around this (by explicitly setting Visible or Enabled
in every single button (eg Button2), but this seems like much more work than
necessary.
Thank you in advance for any direction you can give.
| |
| Phillip Williams 2006-01-31, 7:08 pm |
| A Web application is stateless. A new instance of the Web page class is
created each time the page is requested from the server. This would
ordinarily mean that all information associated with the page and its
controls would be lost with each round trip.
A server control's ViewState is the accumulation of all its property values.
When you set the EnableViewState to false, the Visible value of the textbox
on your page is reset to true (the default) upon every PostBack even if the
PostBack does not execute any commands.
If you want to decide some action based on the state of your textbox you
have to turn the textbox's EnableViewState to true.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Revoemag" wrote:
> I have a very simple WebForm that has 3 Web Controls: 1 TextBox (TextBox1)
> and 2 Buttons (Button1 & Button2).
>
> Button1 checks if the TextBox is Visible (or Enabled) and sets it to False
> (or True in the opposite case). Button2 does absolutely nothing.
>
> If I set EnableViewState=False on the TextBox, I am no longer able to
> control the .Visible or .Enabled properties. Button1 will set .Visible=False,
> but will never set it to True again. Button2 (which does nothing) will bring
> it back up (eg set it back to it's original state).
>
> With EnableViewState=True, it works as expected. Button1 toggles TextBox1,
> Button2 does nothing.
>
> ------------ Code --------------
> <script runat="Server">
>
> Sub Button1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs)
> TextBox1.Visible = Not (TextBox1.Visible)
> End Sub
>
> Sub Button2_Click(ByVal sender As System.Object, ByVal e as System.EventArgs)
> ' Do nothing
> End Sub
> </script>
> <html>
> <head><title>Form Test</title></head>
> <body>
>
> <form Runat="Server">
> <asp:TextBox
> ID="TextBox1"
> EnableViewState="False"
> Runat="Server" /><br />
> <asp:Button
> ID="Button1" Text="Submit"
> OnClick="Button1_Click"
> Runat="Server" />
> <asp:Button
> ID="Button2"
> Text="Do Nothing"
> OnClick="Button2_Click"
> Runat="Server" />
> </form>
>
> </body>
> </html>
> ------------ Code --------------
>
> This is a small example the issue that I use in a much more complex state. I
> can write code to work around this (by explicitly setting Visible or Enabled
> in every single button (eg Button2), but this seems like much more work than
> necessary.
>
> Thank you in advance for any direction you can give.
|
|
|
|
|