Read, With the Name of Your Lord Who Created

Retrieving Query String Values in ASP.NET and Javascript

Posted by triaslama on April 12, 2008

In World Wide Web, a query string is a part of a URL that contains data to be passed to web applications (http://en.wikipedia.org/wiki/Query_string).
The query string is composed of a series of field – value pair. Below is the pattern of query string:
field1=value1&field2=value2&field3=value3

An URL that contains query string can be depicted as follow:
http://test.com/tag?name=alma&role=user
The URL and query string separated by a question mark (?). Query string consist of two parts (field and value), and each of pair separated by ampersand (&). So the example above consists of two query strings which are name and type with value of each field ‘alma’ and ‘user’ respectively.

We can retrieve the query string values programmatically and used that values. Now we will learn how to get the query string values in ASP.NET and Javascript.

Retrieving Query Strings in ASP.NET
Retrieving query strings in ASP.NET is quite simple, what we need is just the following line of code:
Page.Request.QueryString[queryStringField];
or
Page.Request.QueryString[indexOfQueryString];

So If we need to retrieve the value of name field from this URL: http://test.com/tag?name=alma&role=user
Just do this inside of your ASP.NET code: Page.Request.QueryString["name"] or Page.Request.QueryString[0]. Lets look at how it work in simple source of code (Test.aspx):

    <%@ Page Language=”C#” %>
    <html>
    <head>
    <title>Query Strings in ASP.NET: Page 1</title>
    <script language=”C#” runat=”server”>
    void GetQrStr(object sender, EventArgs e)
    {
    // change the Response.Redirect parameter with your own location.
    Page.Response.Redirect(“/…/Test2.aspx?name=alma&role=user”);
    }
    </script>
    </head>
    <body>
    <form runat=”server”>
    <asp:Button id=”btn” onclick=”GetQrStr” text=”Test Query Strings” runat=”server” />
    </form>
    </body>
    </html>

Test.aspx will produce a button that will redirect to Test2.aspx when it clicked. This is code for Test2.aspx:

    <%@ Page Language=”C#”%>

    <html>
    <head>
    <title>Query Strings in ASP.NET: Page 2</title>
    <script language=”C#” runat=”server”>
    </script>
    </head>
    <body>
    <%
    // retrieves query string values
    string name = Page.Request.QueryString["name"];
    string role = Page.Request.QueryString["role"];

    Page.Response.Write(“<h1>Name: “+name+”, Role: “+role+”</h1>”);
    %>
    </body>
    </html>

In Test2.aspx we can get the values of ‘name’ and ‘role’ query strings then use both values as we need.

Retrieving Query Strings in Javascript
Javascript can be used for retrieves query string values too. We need the following code to get query string pairs (field & value) using Javascript:
window.location.search

The above code will return string started with ampersand in the URL (ampersand included). So if we run the above code against the following URL: http://localhost:8088/postings/QrStr/testjs2.htm?name=alma&role=user
The return value is ?name=alma&role=user

To understand the concept better I have write two simple HTML pages that show how to get query string values using Javascript. This is the first file (testqrstr.htm):

    <html>
    <head>
    <title>Test Query Strings: Page 1</title>
    <script lang=”javascript” type=”text/javascript”>
    function testQueryStrings()
    {
    // change the window.location with your own.
    window.location = “/…/testjs2.htm?name=alma&role=user”;
    }
    </script>
    </head>
    <body>
    <input type=”button” id=”btn” value=”Test Query Strings” onclick=”testQueryStrings()” />
    </body>
    </html>

The output produced by testjs.htm is similar like the output of Test.aspx, when we click the button it will bring us to testjs2.htm (of course with the specified query strings!). Here testjs.htm:

    <html>
    <head>
    <title>Test Query Strings: Page 2</title>
    <script lang=”javascript” type=”text/javascript”>
    var qrStr = window.location.search;
    var spQrStr = qrStr.substring(1);
    var arrQrStr = new Array();
    // splits each of pair
    var arr = spQrStr.split(‘&’);

    for (var i=0;i<arr.length;i++){
    // splits each of field-value pair
    var index = arr[i].indexOf(‘=’);
    var key = arr[i].substring(0,index);
    var val = arr[i].substring(index+1);

    // saves each of field-value pair in an array variable
    arrQrStr[key] = val;
    }

    document.write(“<h1>Name parameter: “+arrQrStr["name"]+”. Role parameter: “+arrQrStr["role"]+”</h1>”);
    </script>
    </head>
    <body>
    </body>
    </html>

What I was did in testjs2.htm just retrieve the query strings (through window.location.search), save each of field – value pair in an array variable named arrQrStr and echo the content of arrQrStr variable.

38 Responses to “Retrieving Query String Values in ASP.NET and Javascript”

  1. [...] Retrieving Query String Values in ASP.NET and Javascript [...]

  2. Thanks dear ! It was really helpful what I need .. again thanks a lot and continue these nice code..

  3. Ranjan said

    I was searching for a method to get query string values using javascript and I fall here and I was really glad to find this nicely commented code. It really helped me a lot.

  4. Ranjan said

    Oh! I forget to Thank You Dear..

  5. Adeel Alvi said

    Keep up the good work man , you are really helping people with simplest of way.

  6. Michael said

    Very good Tutorial here my friend. It good to see when someone try to explain a concept they put a piece of code to support it.

  7. Pritesh said

    Nice Article

  8. [...] Posted by ravivarmathumati on March 9, 2009 Retrieving Query String Values in ASP.NET and Javascript [...]

  9. very very nice article

  10. Hello to all !

    police stopped my car
    police auctions in maryland
    santa ana ca police department
    seized car
    florida police seized property
    california auto auctions
    car auction in atlanta
    seized car sales
    repossesed cars for sale
    national auto auction

  11. gvh171 said

    c3c54090f4308fbad51aa96177f82945 29830-eaa8d5d509a5599de7296cd33c27b1d0

  12. manish said

    how r you

  13. I got what exactly i was looking for!
    Thanks

    Waqas Ahmed
    Software & Cloud Developer

  14. Nice easy way to get the querystring in asp.net. Until now, I’ve been doing backflips to get a simple var.

    ———————
    Get the best audience response system for your next meeting…

  15. [...] http://triaslama.wordpress.com/2008/04/12/retrieving-query-string-values-in-aspnet-and-javascript/ [...]

  16. sandy said

    DWQ3kA http://fgb7s3Ffjsev7yrbvqqcf7.com

  17. i am always looking for cheap car sales show in our area and i always find a bargain;:’

  18. Dnrpfepu said

    lol lolita lolitas sites 9225

  19. It’s good blog. Good work.

  20. Cnnatdjm said

    Have you read any good books lately? bbs loli pthc
    >:O

  21. Eogpgmkz said

    Have you got a current driving licence? nn teen
    1987

  22. Fniypjss said

    US dollars Ls Nymphets
    44959

  23. Gfovmgbd said

    Special Delivery Top Models Belgie
    :[[

  24. asp.net, c#,javascript…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  25. Domain Worth Calculator…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  26. just host said

    just host…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  27. car check said

    car check…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  28. finansiera din utbildning…

    [...]8 Finally a person who’s writing about something other than superficioul thin at[...]…

  29. Jolie Dessous…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  30. Wear4you said

    Wear4you…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  31. Ksdloxaj said

    What sort of music do you listen to? http://mybyfufoginy.de.tl young models preenteen Oh yeah she is smokin hot, sweet pussy and yeah fucked like a whore ..she can blow me as well and ride me..

  32. extremely good…the language of explaining is so simple….n i got what i was in a delve for…

  33. awsummmmmmmmmmmmmmmmmmmmmmmm code !!

  34. Game Servers|Server Games|Game Servage|Servage…

    [...]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[...]…

  35. ashok kolwal said

    this is very good example very very thanks for help

  36. You actually make it seem so easy with your presentation but I in finding this matter to be actually
    one thing which I think I might never understand. It sort of feels too complex and very wide for me.
    I am having a look forward in your next submit, I will try to get the dangle of it!

  37. rudresh said

    really nice code………..

  38. jailbreak said

    Hello there, I think your site may be having web browser compatibility problems. When I take a look at your website in Safari, it looks fine however, when opening in IE, it has some overlapping issues. I simply wanted to provide you with a quick heads up! Other than that, fantastic blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: