Global Variable value lost

J

John Mason

Hi,

I am new to asp.net (but not asp 3.0), and am trying to figure out why the
value of a global string variable is not available in a click event
sub-routine.

I declare the global variable directly under the script tag (before the
page_load sub)... as...

Dim globaltext as String

In an asp button click event I assign a value to the variable...(This sub
retrieves a selected value from a data grid)

Sub replaceClicked(sender as Object, e As DataGridCommandEventArgs)
Dim CarColumn as TableCell
CarColumn = e.Item.Cells(2)
globaltext = CarColumn.Text
lblReplace.text = "The globaltext value is " & globaltext & "..." ' This
works!
End Sub

But when I try to retrieve the value in another asp button click event, the
value does not come across...

Sub YesClicked(sender As Object, e As System.EventArgs)
lblReplace.Text = "The globaltext value is " & globaltext & "..." '
This is blank
End Sub

What am I doing wrong? Please help. Thanks in advance!
 
H

Hans Kesting

John Mason said:
Hi,

I am new to asp.net (but not asp 3.0), and am trying to figure out why the
value of a global string variable is not available in a click event
sub-routine.

I declare the global variable directly under the script tag (before the
page_load sub)... as...

Dim globaltext as String

In an asp button click event I assign a value to the variable...(This sub
retrieves a selected value from a data grid)

Sub replaceClicked(sender as Object, e As DataGridCommandEventArgs)
Dim CarColumn as TableCell
CarColumn = e.Item.Cells(2)
globaltext = CarColumn.Text
lblReplace.text = "The globaltext value is " & globaltext & "..." ' This
works!
End Sub

But when I try to retrieve the value in another asp button click event, the
value does not come across...

Sub YesClicked(sender As Object, e As System.EventArgs)
lblReplace.Text = "The globaltext value is " & globaltext & "..." '
This is blank
End Sub

What am I doing wrong? Please help. Thanks in advance!

It is NOT a global variable, it is still declared as an instance-variable
within a class.
The asp.net page translates into a class, and every request is handled by a
fresh
instance of that class.

You could maybe use a "static" variable (note: this is how it is declared in
C#,
VB might have a different keyword), but then everyone accessing this page
would share the same variable, and get the same value. This might not be
what you really want!
One place to store session specific values in in the Session object (who
would
have guessed :)? )

Hans Kesting
 

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

Top