Hi, it seems you're having a little trouble maintaining a value across page
postbacks. I'll see if I can help.
The main difference between Windows Forms and Web Forms, is that Web Forms
uses HTTP to communicate. Now, by design, HTTP is stateless, meaning that it
has no idea, nor does it care, whether postbacks are from the same request /
user. It relays information from the server to the user, and vice versa.
ASP.net tries to blur the line by building in some virtual state management
into the framework. The fact that you use the IsPostBack property of the
Page shows that it's working. Your page knows whether this is a new request,
or you have already been on the page.
Here's a little visualization for the code you are writing. When the user
first accesses the page, it executes the
dim myVariable as string
line. he Page_Load() method is invoked, and you assign a value to it.
myVariable is a simple member of your Page. When the Page is disposed of
(after it is done rendering and sent to the user), your value is destroyed.
So what can you do? Utilize the same features that the ASP.net team created
to hold the value across page postbacks. ViewState. ViewState is an awesome
feature, and you can see evidence of it on your page (look at the HTML
source, and look at the __ViewState hidden HTML input field (it's
encrypted).
The initial values of all the controls on your page (for example, the Text
property of a Label control), are always going to be there. Every time the
Page loads (for the first visit, as well as postbacks), it loads that
Label's Text property from the HTML source. After that, it looks at the
ViewState to see if changes have been made. If so, then it changes it's
value to the ViewState value. For example, if you have a Label that says
"Hello world!", and then have a button which, when clicked, changes the
Label's Text property to "This is my first program!", that Text property of
the Label is stored in ViewState. The next time your page is posted back
(let's say you have another button that doesn't do anything when clicked),
the Page will load "Hello World!" from the HTML source, and then load "This
is my first program!" from the ViewState (which is carried over because it
is a form field).
So, with that little introduction to ViewState, let's get on to solving your
problem. ViewState is completely open, which means you can store and
retrieve whatever values you desire. Here's a working example of what you
want to achieve.
Private Sub Page_Load()
If Not IsPostBack
ViewState("myValue") = "Whatever Value"
End If
End Sub
Private Sub ButtonClick
Dim myVariable As String
If ViewState("myValue") <> Nothing Then
myVariable = ViewState("myValue")
Else
myVariable = String.Empty
End If
' Do whatever you need to
End Function
So basically, on your Page's first load, it will store a value in the
ViewState. At the end of the Page's life cycle (when it renders all it's
content and sends it to the user), it will also save that value you have in
the __ViewState hidden form field. When the page gets posted back, that
encrypted field gets sent to the web server, which can decrypt it and
restore the value you entered into ViewState("myValue").
To answer your question regarding Public and Shared, you'll need to know a
little about access modifiers. There are actually three when you mention
your situation. Public, Shared, and Private. When you write
Dim myVariable as String
it is, by default, private. That means only the methods inside that code
file can access it's value. If you declare it public, then any code file can
access it. However, these only apply if the Page has been instantiated (for
example, when you create an object:
Dim a As MyClass = New MyClass()
you are creating an instance of that class. The Page is also instantiated.
Somewhere in the framework is something along the lines of:
Dim WhateverYourPageName as Page = new Page()
). Shared is a little different, in that it allows you to access it without
the Page being instantiated. I'm actually quite surprised that declaring the
variable as Shared worked, but, I assume, what you're doing is saving a
GLOBAL value to myVariable. Anyone who uses that page will be using the same
value. I'm sure this isn't something you want.
To conclude, try out my code snippet I posted. If you have any more
problems, feel free to reply!
Happy coding,
Johann MacDonagh