……………………………………………….Expertise in .NET Technologies

  • Categories

  • Advertisements

Using QueryString in ASP.NET

Posted by Ravi Varma Thumati on May 5, 2009


Often we need to pass variable content between different html pages or .aspx web forms in context of Asp.Net. For example in first page we collect information about client’s, name and last name and use this information in our 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 we will send 3 parts of information.

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

As you have guessed? Starts our 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 parts builds a query string for our application and send contents of our textboxes to second page. Now how can we retrieve this value in 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 value using their position in the query string. 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 are:

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 we have to send lot information this approach does not work.
  2. QueryString is visible in our address part of our browser so we 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 common problem Asp.Net should have some way to solve. There it is Server.UrlEncode. Server.UrlEncode method changes your query strings 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)); 

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: