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.
Interacting With Get and Post Methods in ASP.NET « Read, With the Name of Your Lord Who Created said
[…] Retrieving Query String Values in ASP.NET and Javascript […]
Abhay Kumar said
Thanks dear ! It was really helpful what I need .. again thanks a lot and continue these nice code..
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.
Ranjan said
Oh! I forget to Thank You Dear..
Adeel Alvi said
Keep up the good work man , you are really helping people with simplest of way.
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.
Pritesh said
Nice Article
Query String in ASP.NET « Microsoft.NET said
[…] Posted by ravivarmathumati on March 9, 2009 Retrieving Query String Values in ASP.NET and Javascript […]
Chandrakant said
very very nice article
detroitautoauction said
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
gvh171 said
c3c54090f4308fbad51aa96177f82945 29830-eaa8d5d509a5599de7296cd33c27b1d0
manish said
how r you
Waqas Ahmed said
I got what exactly i was looking for!
Thanks
Waqas Ahmed
Software & Cloud Developer
response-guy said
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…
State Management « Guru N Guns's said
[…] https://triaslama.wordpress.com/2008/04/12/retrieving-query-string-values-in-aspnet-and-javascript/ […]
sandy said
DWQ3kA http://fgb7s3Ffjsev7yrbvqqcf7.com
Fluorescent Lamp : said
i am always looking for cheap car sales show in our area and i always find a bargain;:’
Dnrpfepu said
lol lolita lolitas sites 9225
Shizuko Moayyad said
It’s good blog. Good work.
Cnnatdjm said
Have you read any good books lately? bbs loli pthc
>:O
Eogpgmkz said
Have you got a current driving licence? nn teen
1987
Fniypjss said
US dollars Ls Nymphets
44959
Gfovmgbd said
Special Delivery Top Models Belgie
:[[
asp.net, c#,javascript said
asp.net, c#,javascript…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
Domain Worth Calculator said
Domain Worth Calculator…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
just host said
just host…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
car check said
car check…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
finansiera din utbildning said
finansiera din utbildning…
[…]8 Finally a person who’s writing about something other than superficioul thin at[…]…
Jolie Dessous said
Jolie Dessous…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
Wear4you said
Wear4you…
[…]Retrieving Query String Values in ASP.NET and Javascript « Read, With the Name of Your Lord Who Created[…]…
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..
nitesh pandey said
extremely good…the language of explaining is so simple….n i got what i was in a delve for…
Waseem Abbas said
awsummmmmmmmmmmmmmmmmmmmmmmm code !!
Game Servers|Server Games|Game Servage|Servage said
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[…]…
ashok kolwal said
this is very good example very very thanks for help
Quitting Cigarettes said
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!
rudresh said
really nice code………..
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!
How To Get Request.querystring In Aspx Page | Siennalydia said
[…] Retrieving Query String Values in ASP.NET and … – Apr 12, 2008 · string name = Page.Request.QueryString … In Test2.aspx we can get the values of ‘name’ and … Retrieving Query String Values in ASP.NET and … […]
India Rupert said
Hi
SEO Link building is a process that requires a lot of time fo wordpress.com
If you aren’t using SEO software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites in proper time and completely automated.
With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you.
With the best user interface ever, you just need to have simple software knowledge and you will easily be able to make your own SEO link building campaigns.
The best SEO software you will ever own, and we can confidently say that there is no other software on the market that can compete with such intelligent and fully automatic features.
The friendly user interface, smart tools and the simplicity of the tasks are making THIS SOFTWARE the best tool on the market.
IF YOU’RE INTERESTED, CONTACT ME ==> seosubmitter@mail.com
Regards,
India