Click here to Skip to main content
11,412,833 members (75,494 online)
Click here to Skip to main content

Passing variables between pages using QueryString

, 18 Jan 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
Pass variables between pages using QueryString and Format with Server.UrlEncode method.


Often you need to pass variable content between your html pages or aspx webforms in context of Asp.Net. For example in first page you collect information about your client, her name and last name and use this information in your second page.

For passing variables content between pages ASP.NET gives us several choices. One choice is using QueryString property of Request Object. When surfing internet you should have seen weird internet address such as one below.

This html addresses use QueryString property to pass values between pages. In this address you send 3 information.

  1. Webform2.aspx this is the page your browser will go.
  2. name=Atilla you send a name variable which is set to Atilla
  3. lastName=Ozgur you send a lastName variable which is set to Ozgur

As you have guessed ? starts your QueryString, and & is used between variables. Building such a query string in Asp.Net is very easy. Our first form will have 2 textboxes and one submit button.

Put this code to your submit button event handler.

private void btnSubmit_Click(object sender, System.EventArgs e)
Response.Redirect("Webform2.aspx?Name=" +
this.txtName.Text + "&LastName=" +

Our first code part builds a query string for your application and send contents of your textboxes to second page. Now how to retrieve this values from second page. Put this code to second page page_load.

private void Page_Load(object sender, System.EventArgs e)
this.txtBox1.Text = Request.QueryString["Name"];
this.txtBox2.Text = Request.QueryString["LastName"];

Request.QueryString is overloaded with a second way. You can also retrieve this values using their position in the querystring. There is a little trick here. If your QueryString is not properly built Asp.Net will give error.

private void Page_Load(object sender, 

System.EventArgs e)
this.txtBox1.Text = Request.QueryString[0];
this.txtBox2.Text = Request.QueryString[1];

Some other ways to reach contents of QueryString.

foreach( string s in Request.QueryString)


for (int i =0;i < Request.QueryString.Count;i++)

Advantages of this approach

  1. It is very easy.

Disadvantages of this approach

  1. QueryString have a max length, If you have to send a lot information this approach does not work.
  2. QueryString is visible in your address part of your browser so you should not use it with sensitive information.
  3. QueryString can not be used to send & and space characters.

If you write this code and try them you will see that you have a problems with space and & characters, e.g. if you need to send a variable which contains & such as "Mark & Spencer". There must be a solution for this problem. If you look to Google’s query string you will see that it contains a lot of %20. This is the solution of our third disadvantage. Replace space with %20 and & with %26 for example.

private void btnSubmit_Click(object sender, System.EventArgs e)
string p1 = this.txtName.Text.Replace("&","%26");
p1 = this.txtName.Text.Replace(" ","%20");
string p2 = this.txtLastName.Text.Replace("&","%26");
p2 = this.txtName.Text.Replace(" ","%20"); 
            "WebForm2.aspx?" + 
            "Name=" + p1 + 
            "&LastName=" + p2;

Since this is a such a common problem Asp.Net should have some way to solve. There it is Server.UrlEncode. Server.UrlEncode method changes your query strings to so that they will not create problems.

 private void btnSubmit_Click(object sender, System.EventArgs e)
Response.Redirect("WebForm2.Aspx?" + 
"Name=" +   Server.UrlEncode(this.txtName.Text) + 
"&LastName=" + Server.UrlEncode(this.txtLastName.Text)); 

Same solution is in Microsoft .Net Quick Start tutorials.

ASP.NET --- Working with Web Controls ---

--- Performing Page Navigation (Scenario 1) ---
--- Performing Page Navigation (Scenario 2) ---

Look at them also if you want to see more example for this technique. Also I advise you to look at Alex Beynenson's article about building QueryString(s).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Atilla Ozgur
Turkey Turkey
I started programming in 1991 with Amiga 68000 Assembler. I am a web and database developer proficient in different languages and databases

Comments and Discussions

GeneralMy vote of 5 Pin
Kedar Kulkarni at 28-Jul-14 1:12
memberKedar Kulkarni28-Jul-14 1:12 
SuggestionHelpful blog Pin
Member 10223004 at 21-Aug-13 3:49
memberMember 1022300421-Aug-13 3:49 
GeneralMy vote of 5 Pin
Member 4734556 at 15-Jun-13 11:03
memberMember 473455615-Jun-13 11:03 
GeneralMy vote of 5 Pin
cs101000 at 23-Dec-12 10:24
membercs10100023-Dec-12 10:24 
GeneralMy vote of 5 Pin
JrPacheco at 8-Nov-12 3:03
memberJrPacheco8-Nov-12 3:03 
GeneralMy vote of 4 Pin
vijayasai Goparaju at 25-Oct-12 2:01
membervijayasai Goparaju25-Oct-12 2:01 
GeneralMy vote of 2 Pin
Nigam Patel at 8-Oct-12 4:54
memberNigam Patel8-Oct-12 4:54 
QuestionRedirecting page particular passing parameter? Pin
Member 9171128 at 18-Sep-12 23:51
memberMember 917112818-Sep-12 23:51 
AnswerRe: Redirecting page particular passing parameter? Pin
Member 9171128 at 26-Sep-12 16:22
memberMember 917112826-Sep-12 16:22 
Questionmy vote of five Pin
alejandro29A at 17-Sep-12 2:36
memberalejandro29A17-Sep-12 2:36 
Generalnice Pin
source.compiler at 11-Aug-12 16:49
membersource.compiler11-Aug-12 16:49 
GeneralMy vote of 5 Pin
lmcatony at 10-Aug-12 8:45
memberlmcatony10-Aug-12 8:45 
Questionhfj Pin
gajendra225 at 6-Jul-12 22:01
membergajendra2256-Jul-12 22:01 
Questionquery string Pin
Ramya.Raju.M at 15-Jun-12 23:17
memberRamya.Raju.M15-Jun-12 23:17 
QuestionQuery String Pin
Sharda Vishwakarma at 18-May-12 22:12
memberSharda Vishwakarma18-May-12 22:12 
AnswerRe: Query String Pin
raikripa at 22-Jun-12 3:18
memberraikripa22-Jun-12 3:18 
GeneralRe: Query String Pin
Member 10105015 at 11-Feb-14 22:02
memberMember 1010501511-Feb-14 22:02 
Suggestioneasy concept of query string Pin
atulkath28 at 4-Apr-12 3:12
memberatulkath284-Apr-12 3:12 
GeneralMy vote of 5 Pin
Sam Path at 21-Feb-12 19:19
memberSam Path21-Feb-12 19:19 
GeneralMy vote of 3 Pin
Kailash_Singh at 4-Nov-11 0:56
memberKailash_Singh4-Nov-11 0:56 
QuestionXML to connect to URL? Pin
tomngs at 9-Sep-11 5:25
membertomngs9-Sep-11 5:25 
QuestionQuery string - nice article Pin
Fergal1970 at 7-Sep-11 10:56
memberFergal19707-Sep-11 10:56 
GeneralMy vote of 5 Pin
lopinti at 5-Jul-11 22:14
memberlopinti5-Jul-11 22:14 
GeneralMy vote of 4 Pin
rmksiva at 30-May-11 0:45
memberrmksiva30-May-11 0:45 
GeneralMy vote of 5 Pin
langkow at 15-Apr-11 4:31
memberlangkow15-Apr-11 4:31 
GeneralMy vote of 4 Pin
Member 3911873 at 2-Feb-11 0:44
memberMember 39118732-Feb-11 0:44 
QuestionHow to display data in a gridview by make use of querystring Pin
Boney1984 at 4-Jan-11 17:01
memberBoney19844-Jan-11 17:01 
GeneralMy vote of 5 Pin
roshan12345678 at 22-Sep-10 21:04
memberroshan1234567822-Sep-10 21:04 
GeneralMy vote of 5 Pin
Sharad Kulkarni at 22-Sep-10 1:36
memberSharad Kulkarni22-Sep-10 1:36 
GeneralMy vote of 1 Pin
sayhello.siddu at 5-Aug-10 23:43
membersayhello.siddu5-Aug-10 23:43 
GeneralQuery Strings Pin
Dursetty Kaveri at 22-Jun-10 23:34
memberDursetty Kaveri22-Jun-10 23:34 
GeneralMy vote of 1 Pin
_Armen at 1-Oct-09 5:30
member_Armen1-Oct-09 5:30 
GeneralExecelent Article Pin
asif hossain shantu at 29-Sep-09 20:12
memberasif hossain shantu29-Sep-09 20:12 
GeneralQuery String Pass Pin
prabusundaram at 23-Jan-09 2:18
memberprabusundaram23-Jan-09 2:18 
GeneralQueryString help Pin
domenicfz at 9-Dec-08 10:07
memberdomenicfz9-Dec-08 10:07 
GeneralHELP!?! Pin
Philly Burton at 8-Oct-07 5:38
memberPhilly Burton8-Oct-07 5:38 
GeneralRe: HELP!?! Pin
pakiyo at 27-Nov-07 17:56
memberpakiyo27-Nov-07 17:56 
GeneralRe: HELP!?! Pin
dotnetwizmanohar at 14-May-12 23:39
memberdotnetwizmanohar14-May-12 23:39 
Generalpassing checkboxlist from page to page Pin
samflex at 14-Jul-07 17:21
membersamflex14-Jul-07 17:21 
QuestionHow to retrieve passing filename ? Pin
Helgeduelbeck at 29-Jun-07 13:29
memberHelgeduelbeck29-Jun-07 13:29 
GeneralPassing from html Pin
Unsichtbar at 22-May-07 7:05
memberUnsichtbar22-May-07 7:05 
QuestionPass through html link variables to results asp page Pin
johnrobinm at 20-Mar-07 18:55
memberjohnrobinm20-Mar-07 18:55 
GeneralRequest.QueryString is Empty while it show in the URL Pin
Karim Rabbani at 14-Mar-07 6:10
memberKarim Rabbani14-Mar-07 6:10 
GeneralRe: Request.QueryString is Empty while it show in the URL Pin
Subodh Ramugade at 21-Dec-11 21:17
memberSubodh Ramugade21-Dec-11 21:17 
Generalplz send me usage of querystring Pin
kale.priya at 9-Jan-07 23:24
memberkale.priya9-Jan-07 23:24 
GeneralRe: plz send me usage of querystring Pin
pakiyo at 27-Nov-07 17:58
memberpakiyo27-Nov-07 17:58 
GeneralHi Pin
AstalaVista Baby at 18-Dec-06 22:44
memberAstalaVista Baby18-Dec-06 22:44 
GeneralQuery String Pin
Pushpa Setty at 26-Oct-06 20:26
memberPushpa Setty26-Oct-06 20:26 
GeneralAbout QueryString Pin
avding at 8-Jun-06 23:15
memberavding8-Jun-06 23:15 
QuestionQuery String Pin
James Robertson at 31-Mar-06 10:35
memberJames Robertson31-Mar-06 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.1 | Last Updated 19 Jan 2004
Article Copyright 2004 by Atilla Ozgur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid