variable problem in ASP.NET

xTwizted13x

New Member
in asp, when I declared a variable, for example "country" that will contain the country name of the user. After that I can use the variable anywhere in the page.<BR><BR>but in ASP.NET, when i declared a variable like this:<BR><BR>Dim country as String<BR>country="Singapore"<BR><BR>after that when i access the value in the <Script language="vb" runat="server"> block, it say that the variable "country" is not declared. Why? Can't we just declatred something that can be use all through the whole script?<BR><BR>Thank you.I didn't have a PC with me that had .NET installed, however these little demo's should work OK (I hope).<BR><BR>I think that they replicate various scenarions that exemplify what you are talking about. Ie: declare a variable, assign it a value, then access that value from within various area in the Page.<BR><BR>You can try them and tell me if they work for you... (If they do work) Work out what you've done differently from what I've got here, and if you have any more questions then, fire away...<BR><BR>'********************* DEMO 1 *************************<BR><BR><% Page language="vb" %><BR><Script language="vb" runat="server"><BR><BR> Dim strCountry As String = "Singapore"<BR><BR> Sub Page_Load (sender As Object, e As EventArgs)<BR> lblFoo.Text = strCountry<BR> End Sub<BR><BR></Script><BR><BR></HTML></BODY><BR><BR> <asp:label id="lblFoo" Runat="server" /><BR><BR></BODY></HTML><BR><BR>'********************* DEMO 2 (with Binding) ******************<BR><BR><% Page language="vb" %><BR><Script language="vb" runat="server"><BR><BR> Dim strCountry As String = "Singapore"<BR><BR> Sub Page_Load (sender As Object, e As EventArgs)<BR> Page.DataBind()<BR> End Sub<BR><BR></Script><BR><BR></HTML></BODY><BR><BR> You live in <%# strCountry %>.<BR><BR></BODY></HTML>I did try out your script (both demo 1 and demo 2)... just one slight problem here.<BR>in my web site, I MUST get the country value BEFORE the <SCRIPT> and not vise versa.<BR><BR>So, the problem is, if i get the country value before the <SCRIPT> tag, it causes the value to be inaccessible inside the <SCRIPT> tag causing inconveniences. The following code illustrate my point:<BR><BR><!=START-----------------------------------------------------------<BR><%<BR>''get the country value from some place, e.g. cookies<BR>dim country as string<BR><BR>country=Request.cookies("my_country")<BR>' assuming that country="Singapore"<BR><BR>%><BR><script language="VB" runat="server"><BR><BR>response.write(country)<BR>''' Cannot, it gives the error<BR>"Compiler Error Message: BC30188: Expected either a variable, constant, Enum, Type, or procedural declaration."<BR><BR>dim country as string<BR>''' strangely this works. Even though I already declared "country" in the code above (<% %>).<BR><BR></script><BR><%<BR>response.write(country)<BR>''' return "Singapore" with no problems.<BR>%><BR><!=END-----------------------------------------------------------<BR><BR>now it looks like the <script> block is completely isolated from the code inside the <% %> delimiter. Now the ASP.NET is driven me asp programmer crazy.... any idea how to change the code so that I can access the value from 'country' normally inside a <SCRIPT> tag? Thanks.
 
Back
Top