About persistance of WebForm class in ASP.NET

  • Thread starter Thread starter Free
  • Start date Start date
F

Free

Hi,

I have a question about ASP.NET :

Here is an example of WebForm code. 2 buttons and 1 textbox.
----------------------------------------------------------------------------
-----------------------
Public Class MyWebPage
Inherits System.Web.UI.Page

Protected WithEvents MyButton1 As System.Web.UI.WebControls.Button
Protected WithEvents MyButton2 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

' Web Form Designer Generated Code

Private m_strMyString As String

Private Sub MyButton1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyButton1.Click
m_strMyString = "Hello"
End Sub

Private Sub MyButton2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyButton2.Click
TextBox1.Text = m_strMyString
End Sub

End Class
----------------------------------------------------------------------------
-------------------------

When I click MyButton1, I set m_strMyString to "Hello" but after that, if I
click on MyButton2, m_strMyString is Nothing.
Each time I return to server all variables are erased.
Is it normal ?
Is ther a way to correct this without using Session variables or shared
variables ?

Thanks a lot

Guillaume
 
This is how the web works, it is stateless - designed to forget it's past.
You would need to explicitly store it somewhere to persist the info.
regards
 
I had the same problem. I couldn't find my previous post, but you need to
add the variable to the viewstate. The viewstate syntax is the same as the
session object, but the viewstate is stored on the page in encrypted form.

Viewstate("Value") = myVariable

myVariable = Viewstate("Value")
 
Back
Top