simple toggle variable in asp.net

  • Thread starter Thread starter Jordan Bowness
  • Start date Start date
J

Jordan Bowness

Is asp.net able to persist local variables using viewstates?

I'm just trying to get a simple toggle working. Here's my code:



Public Class WebForm1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public mfToggle As Boolean

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
mfToggle = Not mfToggle
Button1.Text = mfToggle
End Sub
End Class




Every time the page posts back on the Button1.click event, it just
results in the local variable mfToggle being reset to False.

Many thanks.

================
Jordan Bowness
================
 
I believe I misunderstood your question. Local variables don't
maintain state. You can put the value in a hidden input type (<input
type=hidden... or the codebehind equivalent with the proper object), a
session variable (session("mtoggle")="true"), or a cookie
(response.cookies("mtoggle")="true"). The cookie option is probably
the best because it is extensible.

Don Krapohl
http://www.tranquillitas.com
Website quality assurance testing
 
Back
Top