Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to use Query String to pass values between ASP.NET Pages

Author: Nikki Smith

Query String is a way to pass values from one page to another in ASP.NET. I will explain it with a simple example in this article.


Although Query String is not a best way to pass values between ASP.NET pages but it is very useful in some situations. We append values in the URL of the page to pass to that page and then retrieve these values in that page. This technique has security issues because we show our values to the user in URL. Also, most browsers limit the URL to 255 characters. The Key-Value pair is used to pass values. First Query String variable is appended in the URL starting with question mark (?) and after that every other variable is appended using “and” sign (&). “Equals” sign (=) is used between each Query String Key-Value pair.

You will learn to use Query String to pass values between ASP.NET pages with a simple example.
  1. Create a new Web Site in Visual Studio 2010 either in Visual Basic or Visual C#.
  2. Add two Web Forms in the Web Site. Name First Web Form as Page1.aspx and second as Page2.aspx
  3. Write code below in Page1.aspx page. I have added 2 Labels, 2 Textboxes and 1 Button

    <asp:Label ID="lblName" runat="server" Text="Name:"></asp:Label>
    <asp:TextBox ID="txtboxName" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="lblCity" runat="server" Text="City:"></asp:Label>
    <asp:TextBox ID="txtBoxCity" runat="server"></asp:TextBox>
    <br />
        <asp:Button ID="btnQueryStringValues" runat="server" Text="Click To See Query String Values on Page2" Width="270px" onclick="btnQueryStringValues_Click" />

  4.  Now add Button click event in code behind file of Page1

    Visual Basic

    Protected Sub btnQueryStringValues_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQueryStringValues.Click
         Dim name As String = txtboxName.Text
         Dim city As String = txtBoxCity.Text
         Response.Redirect("Page2.aspx?Name=" + name + "&City=" + city)
    End Sub
    Visual C#

    protected void btnQueryStringValues_Click(object sender, EventArgs e) {
        string name = txtboxName.Text;
        string city = txtBoxCity.Text;
        Response.Redirect("Page2.aspx?Name=" + name + "&City=" + city);

    Name and City values are appended in the URL using Key-Value pair. We need the “Key for every value to retrieve Query String variables in the page.  
  5. Write code below in Page2.aspx page. I have added a Label and a Hyper Link.

    <asp:Label ID="lblQueryStringValues" runat="server" ></asp:Label>
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Page1.aspx">Back to Page1</asp:HyperLink>

  6.  Now add code below in Page Load method in code behind file of Page2

    Visual Basic

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim name As String = Request.QueryString("Name")
        Dim city As String = Request.QueryString("City")
        lblQueryStringValues.Text = "Your name is: " + name + " and City is: " + city
    End Sub
    Visual C#
    protected void Page_Load(object sender, EventArgs e) {
        string name = Request.QueryString["Name"];
        string city = Request.QueryString["City"];
        lblQueryStringValues.Text = "Your name is: " + name + " and City is: " + city;

    Notice here we have used “Key” to retrieve Query String variables.
  7. Right click on Page1.aspx and Set As Start Page. Press F5 and see the result in browser.


Add Article Comment:
Name :
Email Address :
Comments :
<< How to use repeater control in ASP.NET

Disclaimer - Privacy
© 2002-2017