|
 |
asp_web_howto thread: Initializing Variables
Message #1 by "Chris Messina" <cmessina+wrox@a...> on Sun, 8 Sep 2002 17:55:14
|
|
I have a semi-newbie question which may be a bit more complicated than I
realize. I have a database-driven ASP site with a table containing all the
site's URLs that I use to generate things like links, page titles, etc,
throughout the site. Each page's content relies on those attributes which
I grab from the URL with the request.QueryString method.
My problem lies in that when you enter the site from the homepage
(index.asp) or you go to a page without clicking on a link from within the
site, none of the page's attributes are set and thus the page design
crumbles. I realize that this may have a been a poor way to build the
site, but I'm learning as I go along and I don't know the *proper* way of
developing the programming.
What I think I need to do is test to see if the main attribute which
determines the rest of the page's attributes is set (the URLID), and if
not, redirect to a page where the attributes _have_ already been set. I
would think that setting the variables _without_ redirecting would be
optimal, but I don't know how to do this without getting the "Variables
have already been been defined" (or something) error.
Here's the code I'm currently using in my general include file, which of
course will redirect you to the index, no matter which page you go to
where the page values aren't set:
<%
if IsEmpty(request.queryString("URLID")) then
Response.redirect("index.asp?URLID=44")
end if
%>
Sorry if this isn't clear. The length of this post should illustrate how
little I even understand about the problem.
-Chris
Message #2 by "Ken Schaefer" <ken@a...> on Mon, 9 Sep 2002 14:13:18 +1000
|
|
Hi Chris,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Chris Messina" <cmessina+wrox@a...>
Subject: [asp_web_howto] Initializing Variables
: What I think I need to do is test to see if the main attribute which
: determines the rest of the page's attributes is set (the URLID), and if
: not, redirect to a page where the attributes _have_ already been set. I
: would think that setting the variables _without_ redirecting would be
: optimal, but I don't know how to do this without getting the "Variables
: have already been been defined" (or something) error.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Without seeing exactly what the error is I'm guessing a bit here. But from
the rather involved post I think I see what your problem is.
When designing an app you should try to "split" it into lots of little
chunks. Each chunk being as self contained/modular as possible.
The way I would handle this is:
a) Don't have "inline" code in side your include files. Have routines
(functions/subs) instead. This way you can put all your includes into one
place (I like to put them together at the top of the page). You can call
your routines as many times as you need in the one page (to re-use code),
and you don't have to worry about having a variable name defined more than
once - all variables are dimmed in the main calling page.
b) Your code would look something like:
<%
Option Explicit
%>
<!-- #include virtual="/includes/general/main.asp" -->
<!-- #include virtual="/includes/general/constants.asp" -->
<%
' Above contains all your "general" routines to go in each page
' The second include would be the only one where you define/DIM
' any variables - it should include global constants
' (or you could store them as application variables
' You can split them up how you want though
' Dim all "global" variables here
Dim intURLID ' as Integer, holds value of link record ID
Dim objCon ' as ADODB.Connection (you get the idea)
' Get incoming values:
intURLID = Request.QueryString("URLID")
strSomeOtherVar = Request.QueryString("OtherVariable")
If not isValidURLID(intURLID) then
intURLID = URL_ID
' (constants are usually in UPPER CASE, but in your case
' if would be difficult to separate from a variable URLID!)
End If
' Now we use intURLID to get the data
' Since we know we have a value intURLID
strSQL = _
"SELECT... " & _
"FROM TableHere " & _
"WHERE URLID = " & intURLID
%>
In your include file:
<%
Function isValidURLID ( _
ByVal intIDToTest _
)
If not IsNumeric(intIDToTest) or Len(intIDToTest & "") = 0 then
isValidURLID = False
Else
If Abs(Int(intIDToTest)) <> intIDToTest then
isValidURLID = False
Else
isValidURLID = True
End If
End Function
%>
and in the other
CONST URL_ID = 44
HTH
Cheers
Ken
Message #3 by jeff.montgomery@m... on Thu, 12 Sep 2002 04:55:36
|
|
Ken Schaefer's advice is right on target. Really, it sounds like the only
thing you're doing wrong is trying to Dim the variables twice, which you
can't do (that will give you a "Name redefined" error). You're right that
you don't have to redirect simply to initialize your variables. On the
index page, just check to see if a variable is empty, and if it is, give
it a default value.
Jeff Montgomery
jeff.montgomery@m...
> I have a semi-newbie question which may be a bit more complicated than I
r> ealize. I have a database-driven ASP site with a table containing all
the
s> ite's URLs that I use to generate things like links, page titles, etc,
t> hroughout the site. Each page's content relies on those attributes
which
I> grab from the URL with the request.QueryString method.
> My problem lies in that when you enter the site from the homepage
(> index.asp) or you go to a page without clicking on a link from within
the
s> ite, none of the page's attributes are set and thus the page design
c> rumbles. I realize that this may have a been a poor way to build the
s> ite, but I'm learning as I go along and I don't know the *proper* way
of
d> eveloping the programming.
> What I think I need to do is test to see if the main attribute which
d> etermines the rest of the page's attributes is set (the URLID), and if
n> ot, redirect to a page where the attributes _have_ already been set. I
w> ould think that setting the variables _without_ redirecting would be
o> ptimal, but I don't know how to do this without getting the "Variables
h> ave already been been defined" (or something) error.
> Here's the code I'm currently using in my general include file, which of
c> ourse will redirect you to the index, no matter which page you go to
w> here the page values aren't set:
> <%
> if IsEmpty(request.queryString("URLID")) then
> Response.redirect("index.asp?URLID=44")
> end if
%> >
> Sorry if this isn't clear. The length of this post should illustrate how
l> ittle I even understand about the problem.
> -Chris
|
|
 |