5,699,997 members and growing! (24,723 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

Passing variables between pages using QueryString

By Atilla Ozgur

Pass variables between pages using QueryString and Format with Server.UrlEncode method.
C#, Windows, .NET 1.0, .NET 1.1, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 18 Jan 2004
Updated: 18 Jan 2004
Views: 367,942
Bookmarked: 33 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
48 votes for this Article.
Popularity: 6.58 Rating: 3.91 out of 5
2 votes, 4.3%
1
3 votes, 6.5%
2
1 vote, 2.2%
3
11 votes, 23.9%
4
29 votes, 63.0%
5

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.

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.

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)
{
Response.Write(Request.QueryString[s]);
}

Or 

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.

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.

 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)

About the Author

Atilla Ozgur


I started programming in 1991 with Amiga 68000 Assembler. I am a web and database developer proficient in different languages and databases
Occupation: Web Developer
Company: Turksat
Location: Turkey Turkey

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 32 (Total in Forum: 32) (Refresh)FirstPrevNext
GeneralHELP!?!memberPhilly Burton5:38 8 Oct '07  
GeneralRe: HELP!?!memberpakiyo17:56 27 Nov '07  
Generalpassing checkboxlist from page to pagemembersamflex17:21 14 Jul '07  
GeneralHow to retrieve passing filename ?memberHelgeduelbeck13:29 29 Jun '07  
GeneralPassing from htmlmemberUnsichtbar7:05 22 May '07  
QuestionPass through html link variables to results asp pagememberjohnrobinm18:55 20 Mar '07  
GeneralRequest.QueryString is Empty while it show in the URLmemberKarim Rabbani6:10 14 Mar '07  
Generalplz send me usage of querystringmemberkale.priya23:24 9 Jan '07  
GeneralRe: plz send me usage of querystringmemberpakiyo17:58 27 Nov '07  
GeneralHimemberAstalaVista Baby22:44 18 Dec '06  
GeneralQuery StringmemberPushpa Setty20:26 26 Oct '06  
GeneralAbout QueryStringmemberavding23:15 8 Jun '06  
QuestionQuery StringmemberJames Robertson10:35 31 Mar '06  
GeneralRequest Querystring in asp.netmembervkancherlapalli@linkageinc.com7:53 14 Jul '05  
GeneralRe: Request Querystring in asp.netmemberSteve Maier8:14 14 Jul '05  
GeneralRe: Request Querystring in asp.netmembervkancherlapalli@linkageinc.com8:17 14 Jul '05  
GeneralRe: Request Querystring in asp.netmemberSteve Maier8:46 14 Jul '05  
GeneralRe: Request Querystring in asp.netmembervkancherlapalli@linkageinc.com9:15 14 Jul '05  
GeneralRe: Request Querystring in asp.netmemberSteve Maier9:51 14 Jul '05  
GeneralRe: Request Querystring in asp.netmemberAtilla Ozgur2:16 16 Jul '05  
GeneralHiding these variables from the user?sussAnonymous17:52 23 Jun '05  
GeneralRe: Hiding these variables from the user?memberjhered9:41 14 Dec '06  
AnswerRe: Hiding these variables from the user?memberthanei3:35 18 Jul '07  
GeneralSpecial CharactermemberAubyone13:53 23 May '05  
GeneralRe: Special CharactermemberVasudevan Deepak Kumar8:40 14 Jul '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 18 Jan 2004
Editor: Nishant Sivakumar
Copyright 2004 by Atilla Ozgur
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project