-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection




 Lesson 15: Query strings, part I
Lesson 15: Query strings, part I

Author: Alexander Haneng
Difficulty: Easy
Requires: ASP
Demo: demo
Download: Lesson15.zip
Summary:
Learn to use query strings to pass information between ASP pages. View the demo to see the result.

Intro:
Often when you build web applications you need to pass information from one page to another. This can be done in four ways: cookies, session variables, forms and query strings. Query strings are a way to pack information into a link, and then retrieve that information on the page that was linked to. Let me show you: A regular URL to a page is like this page.asp. The URL with a query string might look like this page.asp?id=1. See the difference? We have added ?id=1 to the URL. But how do we retrieve such information in the next page. It's pretty easy, just use:
<%
Query_ID = Request.QueryString("ID")
%>
The value is now stored in our variable Query_ID. And if the url to the page had the query string ?id=1, then Query_ID is now 1. Let's try it out.
Let's take apart the code for this little demo. First we have the link page:
<HTML>
<BODY>
<B>Click one of the links below:</B><BR>
<A HREF="page.asp?link=1">Link 1</A><BR>
<A HREF="page.asp?link=2">Link 2</A><BR>
<A HREF="page.asp?link=3">Link 3</A><BR>
<A HREF="page.asp?link=4">Link 4</A><BR>
<A HREF="page.asp?link=5">Link 5</A>
</BODY>
</HTML>
As you see so have I used a query string in every URL to mark which linked is being clicked. Let's see the code behind page1.asp:
<HTML>
<BODY>
You clicked link number <%=Request.QueryString("link")%>!<BR><BR>
<A HREF="default.asp">Try again</A>
</BODY>
</HTML>
Here we retrive the query string, and then outputs it directly, telling the user which link he have clicked.

Encoding a query string:
When you use query strings, you must URL-encode it before you pass it. All spaces must be converted to addition signs etc. If you don't URL-encode it, you may get some very strange results. To URL-encode something with ASP is very easy, just use Server.URLEncode(). Let's try it on this sentence: "Some info that I need.":
<A HREF="page.asp?sentence=<%=Server.URLEncode("Some info that I need.")%>">Click here!</A>
After URL encoding, the sentence will look like this: Some+info+that+I+need%2E You don't have to URL-encode the name of neither the query string nor the equal sign, just the value.

Decoding a query string:
Decoding a query string couldn't be easier: ASP does it for you! So the sentence from our last example would be displayed normally if we on the next page used:
<%=Request.QueryString("Sentence")%>

More query strings
Learn to use multiple parameters and multiple values, dumping the content of the QueryString collection and when not to use query strings: Lesson 16.

Where to go next:
Check out the other lessons about parsing values between ASP pages: Lesson 8: Cookies and Lesson 13: Forms.


14: EZ update!
16: Query strings, part II
| Info |
© Copyright 1997-2013 Alexander Haneng