TheGAME1264
April 14 '04, 05:11 PM
In my years as an ASP developer, I've gotten into the habit of using a querystring called "action" on my various pages to determine the appropriate action a page is to take.
For example, I may have an ASP page that looks something like this:
sub Procedure1
...
end sub
sub Procedure2
...
end sub
sub PageHeader
(page header code)
end sub
sub PageFooter
(page footer code)
end sub
Dim Action
Action = Request.QueryString ("action")
if Action = "" then
Action = "procedure1"
end if
PageHeader
if Action = "procedure1" then
Procedure1
elseif Action = "procedure2" then
Procedure2
end if
PageFooter
In this very simple example, the problem isn't immediately apparent. But let's say we added Procedure3, Procedure4, Procedure5, Procedure6, etc. to this page. The series of if statements associated with the Action statement would quickly add up and become rather cumbersome.
Enter the "Execute" function. Execute is a built-in ASP function that will allow the programmer to execute a line of ASP code that is passed to it.
The syntax is as follows:
Execute (line of ASP code)
For example:
Execute (Response.Write "Hello")
Would execute the line Response.Write "Hello", and as a result, "Hello" would be outputted to the screen.
Going back to our example above, let's replace all of those simple if statements with one execute line. You will notice that I have used values for the Action querystring that correspond with their associated procedures. Besides being a good programming practice (since it tells another programmer what's going to be called), it can be used in conjunction with the Execute command to reduce code significantly.
How is this done? Let's take our example above and make one modification:
sub Procedure1
...
end sub
sub Procedure2
...
end sub
sub PageHeader
(page header code)
end sub
sub PageFooter
(page footer code)
end sub
Dim Action
Action = Request.QueryString ("action")
if Action = "" then
Action = "procedure1"
end if
PageHeader
Execute (Action)
PageFooter
As you can see, we've replaced five lines of if statements with a single line that will execute whatever the value of Action may be.
Those of you who are more perceptive will have noticed that I assigned a default value to Action. This is done to ensure that something gets executed; if Action remained null, then an error would be generated.
In the next post, I will provide another huge timesaver when it comes to retrieving information for a specific record.
For example, I may have an ASP page that looks something like this:
sub Procedure1
...
end sub
sub Procedure2
...
end sub
sub PageHeader
(page header code)
end sub
sub PageFooter
(page footer code)
end sub
Dim Action
Action = Request.QueryString ("action")
if Action = "" then
Action = "procedure1"
end if
PageHeader
if Action = "procedure1" then
Procedure1
elseif Action = "procedure2" then
Procedure2
end if
PageFooter
In this very simple example, the problem isn't immediately apparent. But let's say we added Procedure3, Procedure4, Procedure5, Procedure6, etc. to this page. The series of if statements associated with the Action statement would quickly add up and become rather cumbersome.
Enter the "Execute" function. Execute is a built-in ASP function that will allow the programmer to execute a line of ASP code that is passed to it.
The syntax is as follows:
Execute (line of ASP code)
For example:
Execute (Response.Write "Hello")
Would execute the line Response.Write "Hello", and as a result, "Hello" would be outputted to the screen.
Going back to our example above, let's replace all of those simple if statements with one execute line. You will notice that I have used values for the Action querystring that correspond with their associated procedures. Besides being a good programming practice (since it tells another programmer what's going to be called), it can be used in conjunction with the Execute command to reduce code significantly.
How is this done? Let's take our example above and make one modification:
sub Procedure1
...
end sub
sub Procedure2
...
end sub
sub PageHeader
(page header code)
end sub
sub PageFooter
(page footer code)
end sub
Dim Action
Action = Request.QueryString ("action")
if Action = "" then
Action = "procedure1"
end if
PageHeader
Execute (Action)
PageFooter
As you can see, we've replaced five lines of if statements with a single line that will execute whatever the value of Action may be.
Those of you who are more perceptive will have noticed that I assigned a default value to Action. This is done to ensure that something gets executed; if Action remained null, then an error would be generated.
In the next post, I will provide another huge timesaver when it comes to retrieving information for a specific record.