Click here to Skip to main content
15,881,559 members
Articles / Web Development / ASP.NET
Article

Passing variables between pages using QueryString

Rate me:
Please Sign up or sign in to vote.
4.63/5 (110 votes)
18 Jan 2004CPOL3 min read 1.8M   8.3K   94   69
Pass variables between pages using QueryString and Format with Server.UrlEncode method.

Introduction

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.

http://www.localhost.com/Webform2.aspx?name=Atilla&lastName=Ozgur

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.

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

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.

C#
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.

C#
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.

C#
foreach( string s in Request.QueryString)
{
Response.Write(Request.QueryString[s]);
}

Or 

C#
for (int i =0;i < Request.QueryString.Count;i++)
{
Response.Write(Request.QueryString[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.

C#
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;
Response.Redirect(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.

C#
 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).

License

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


Written By
ISKUR
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 Kulkarni28-Jul-14 0:12
Kedar Kulkarni28-Jul-14 0:12 
SuggestionHelpful blog Pin
Member 1022300421-Aug-13 2:49
Member 1022300421-Aug-13 2:49 
GeneralMy vote of 5 Pin
Member 473455615-Jun-13 10:03
Member 473455615-Jun-13 10:03 
GeneralMy vote of 5 Pin
cs10100023-Dec-12 9:24
cs10100023-Dec-12 9:24 
GeneralMy vote of 5 Pin
Júnior Pacheco8-Nov-12 2:03
professionalJúnior Pacheco8-Nov-12 2:03 
GeneralMy vote of 4 Pin
vijayasai Goparaju25-Oct-12 1:01
vijayasai Goparaju25-Oct-12 1:01 
GeneralMy vote of 2 Pin
Nigam Patel8-Oct-12 3:54
Nigam Patel8-Oct-12 3:54 
QuestionRedirecting page particular passing parameter? Pin
Member 917112818-Sep-12 22:51
Member 917112818-Sep-12 22:51 
AnswerRe: Redirecting page particular passing parameter? Pin
Member 917112826-Sep-12 15:22
Member 917112826-Sep-12 15:22 
Questionmy vote of five Pin
alejandro29A17-Sep-12 1:36
alejandro29A17-Sep-12 1:36 
Generalnice Pin
ali_heidari_11-Aug-12 15:49
ali_heidari_11-Aug-12 15:49 
GeneralMy vote of 5 Pin
lmcatony10-Aug-12 7:45
lmcatony10-Aug-12 7:45 
Questionhfj Pin
gajendra2256-Jul-12 21:01
gajendra2256-Jul-12 21:01 
Questionquery string Pin
Ramya.Raju.M15-Jun-12 22:17
Ramya.Raju.M15-Jun-12 22:17 
QuestionQuery String Pin
Sharda Vishwakarma18-May-12 21:12
Sharda Vishwakarma18-May-12 21:12 
AnswerRe: Query String Pin
raikripa22-Jun-12 2:18
raikripa22-Jun-12 2:18 
GeneralRe: Query String Pin
Member 1010501511-Feb-14 21:02
Member 1010501511-Feb-14 21:02 
Suggestioneasy concept of query string Pin
atulkath284-Apr-12 2:12
atulkath284-Apr-12 2:12 
GeneralMy vote of 5 Pin
Sam Path21-Feb-12 18:19
Sam Path21-Feb-12 18:19 
GeneralMy vote of 3 Pin
Kailash_Singh3-Nov-11 23:56
professionalKailash_Singh3-Nov-11 23:56 
QuestionXML to connect to URL? Pin
tomngs9-Sep-11 4:25
tomngs9-Sep-11 4:25 
QuestionQuery string - nice article Pin
Fergal19707-Sep-11 9:56
Fergal19707-Sep-11 9:56 
GeneralMy vote of 5 Pin
appaLop5-Jul-11 21:14
appaLop5-Jul-11 21:14 
GeneralMy vote of 4 Pin
rmksiva29-May-11 23:45
professionalrmksiva29-May-11 23:45 
GeneralMy vote of 5 Pin
langkow15-Apr-11 3:31
langkow15-Apr-11 3:31 

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

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