Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Scope 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
How do I declare a variable that will be available to me to every sub on my page?

I tried putting the Dim statement before the first Sub, after the Script command, but when the page returns from the server, it's blank.

I'm using 1 calendar control to update 2 textboxes depending on which button I press.

<%@ Page Language=&quot;VB&quot; %>
<script runat=&quot;server&quot;>

' Insert page code here
'
Private byWhichDate As Byte 'THIS DOES NOT WORK - IT DOES NOT KEEP IT'S NUMBER.

Sub Page_Load(Sender As Object, E As EventArgs)

If Not Page.IsPostBack Then

End If

End Sub


Sub btnShowCallDateCalendar_Click(sender As Object, e As EventArgs)
response.write(byWhichDate)
If calCallDate.Visible = False Then
calCallDate.Visible = True
byWhichDate = 1
Else
calCallDate.Visible = False
byWhichDate = 0
End If
End Sub

Sub btnShowNextCallDateCalendar_Click(sender As Object, e As EventArgs)
response.write(byWhichDate)

If calCallDate.Visible = False Then
calCallDate.Visible = True
byWhichDate = 2
Else
calCallDate.Visible = False
byWhichDate = 0
End If
End Sub

Sub btnAddDetail_Click(sender As Object, e As EventArgs)

End Sub

Sub btnCancel_Click(sender As Object, e As EventArgs)
Response.Redirect(&quot;AgwayMaster.aspx&quot;)
End Sub

Sub calCallDate_SelectionChanged(sender As Object, e As EventArgs)
response.write(byWhichDate)
Select byWhichDate
Case 1
txtCallDate.Text = calCallDate.SelectedDate
calCallDate.Visible = False
Case 2
txtNextCallDate.Text = calCallDate.SelectedDate
calCallDate.Visible = False
Case Else
End Select
End Sub

</script>
<html>
<head>
</head>
... Here is the 2 textboxes and a calender control.
 
Do I need to use a textbox.visible=false to keep the data (0/1/2) from postback to postback?
 
Spaghetti code has wierd scoping rules. My understanding (and I absolutely don't use it at all, so this is strictly from memory of nearly two years ago learning it) is that there's no such thing as a &quot;global&quot; variable in that respect when using spaghetti code...

Variables declared outside of a procedure have scope only w/in other <%%> blocks and only outside of other procedures. Variables declared inside procedures, obviously, only have scope w/in that procedure, unless stuffed into Session[], ViewState[], or some other state management object.

I don't exactly understand why this works this way, but only good things can come from moving to a code-behind design pattern. My strong suggestion would be to move in that direction.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Paul, help me out here.... I thought ASP.NET WAS a code behind!?!

What do I need to learn. I'm just getting into ASP.NET from the ASP world. Know VB too.
 
Code-behind is where your interface (aspx) is kept in a separate file from your code (aspx.cs or aspx.vb)

where the codebehind file is a class that the page uses to fetch it's events/brains/etc...

Here is a load of information on the subject:


-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks Paul. I'm just getting into the .net world coming from asp.
 
Welcome, bigfoot. The weather is always 82 degrees and sunny here, compared to 42 and raining in the ASP Classic neighborhood.

I think you'll like it here.

[lol]

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
WOW! I was reading some stuff from the 4 guys from rolla. Code behind is like VB.

I used VB 3,5, & 6. ASP was like BASICA, or worse. ASP>NET is like coming home again. :)

The more I use it, the more I like it.
 
The variable relates to that particular page serving. So the server runs the code, serves out the HTML and then clears itself. It doesn't remember a thing because it's not meant to! That's what the Session is for, saving state.

Craig
 
This looks better and better!
So the page calls a class's public function/method?

And you can compile the code!?!? WOW!!

I haven't felt this way since I got my first &quot;Hello World&quot; program to run on my Commodore Vic20 many, many years ago.

This is amazing!

Thanks Paul, you get a start for this, and 2 if I can figure out how. :-D

-Gary
 
Even with code behind, it STILL does the same thing!

How do I save a variable's value from one click to another??
 
If it's only needed on the same page, then you can place it into ViewState

to assign:
ViewState(&quot;varName&quot;) = varName

to retrieve:
varName = ViewState(&quot;varName&quot;)

or to save it from one page to another, either
(a)pass it via querystring (low resources, low security)
(b)store it in session (higher resources, higher security)

There are other ways, and I always suggest querystring whenever possible. Session should be used very sparingly to conserve resources, and only ever to store simple variables (ints, strings, etc...), never complicated objects (custom classes other than structs), which require more memory.

Session can be used to store these objects, but should not be used in that fashion.

As to why the variable isn't saved, ASP.NET is still a stateless medium, just like any web based technology is (and just like ASP Classic is).

ViewState is the resource equivalent to passing things via querystring, since they wind up in a hidden variable that's posted back just like anything else. The nice thing is that it's bundled quite nicely into the ViewState object automatically w/o any special gyrations on your part.

Let me know if this gets you going.

:)
-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Well, what can I say Paul. You got me out of a jam again.
I'll be oweing you for the .NET lessons soon. :~)

It worked great. I'll have to remember this little trick.
I'm keeping notes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top