About persistance of WebForm class in ASP.NET

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
 
A

Alvin Bruney

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
 
B

Brian

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")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top