SIGN UP MEMBER LOGIN:    
ARTICLE

Simple Explanation for very beginners of Query String

Posted by Dhananjay Kumar Articles | C# Language May 10, 2010
Query String allows you to pass values between the asp.net pages.
Reader Level:

Query String allows you to pass values between the asp.net pages.

Let us say, you are navigating to below URL

QueryString1.gif

In above URL, you are navigating to a page called Add.aspx and Number1 and Number2 are query string to page Add.aspx.

To create Query String,

  1. Add a question mark (?) to URL.
  2. After Question mark, give variable name and value separated by equal to (=).
  3. If more than one parameter need to be passed then separate them by ampersand (&).
To read query String,
Say your URL is,
http://localhost:12165/Add.aspx?Number1=72&Number2=27

Then to read query string, on Add.aspx

QueryString2.gif

You can read by passing exact variable name in index or,

QueryString3.gif

You can read, by passing index value as above.

Now, I am going to show you a very basic sample,
  1. I will create a page with two text boxes and one link button.
  2. I will pass text boxes values as query string to other page on click event of link button.
  3. On the other page, I will read the query string value and will add them in display in a label.
I have created a new asp.net application. Then I dragged and dropped two textboxes and one link button on the page. On click event of link button, I am performing the below operation.

QueryString4.gif

I am constructing the URL, as to navigate to Add.aspx page and then appending two query strings from textbox1 text and textbox2 text.

Now, add a page to project say Add.aspx . Drag and drop a label on the page. On the page load event perform the below operation.

QueryString5.gif

So, when you run the application you will get the output as below,


QueryString6.gif

For your reference, the whole source code is given below,

Default.aspx.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespacequeryparameterexample
{
publicpartialclass_Default : System.Web.UI.Page
    {
protectedvoidPage_Load(object sender, EventArgs e)
        {
 
        }

protectedvoid LinkButton1_Click(object sender, EventArgs e)
        {
stringurltoNaviget = "~/Add.aspx?Number1=" +
                                     TextBox1.Text +
"&Number2=" +
                                     TextBox2.Text;
 
            LinkButton1.PostBackUrl = urltoNaviget;
        }
    }
}

Add.aspx.cs

using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;

namespacequeryparameterexample
{
publicpartialclassWebForm1 : System.Web.UI.Page
    {
protectedvoidPage_Load(object sender, EventArgs e)
        {

string number1 = Request.QueryString[0];
string number2 = Request.QueryString[1];

// The other way to Read the values
//string number1 = Request.QueryString["Number1"];
//string number2 = Request.QueryString["Number2"];

int result = Convert.ToInt32(number1) + Convert.ToInt32(number2);
            Label2.Text = result.ToString();
 
        }
    }
}


I hope this post is useful. Thanks for reading. Happy Coding. 

Login to add your contents and source code to this article
share this article :
post comment
 

hi i have a doubt on this In pagelife cycle in which event the Query String fires help me?

Posted by Naresh Babu Gopavaram Mar 01, 2011

hi sir,
plzz help. i am trying to pass category_id from 1st page to 2nd page and from 2nd page i want to pass category_id and product_id from 2nd page to 3rd page.. in 3rd page i want category_id and product_id..
i am not getting it

Posted by B M Suchitra Dec 04, 2010

Sir, thank you for posting this.. Very helpful.. But what if I want to pass a variable that has text. not number. for example I want to pass Employee name to page 2. what value I'll declare to that inside the URL?

thank you

Posted by Heaven Valenzona May 22, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor