Query String Example in Asp .Net

In this Asp .Net tutorial we will learn how to use Query Strings. Query Strings are used to send data from one page of website to another page.In this article I have used single parameter in query string however you can send multiple parameters in query strings as name value pair.  


Note: It is very insecure to send data using query strings as data is viewable to users and they can change it easily. However you can Encrypt and Decrypt query string parameters to secure query string parametersl. You can read this detailed article:
How to Encrypt and Decrypt query string in asp .net.


Step1: Create a new asp .net website.


Step2: Add new webpage in your website.

I have created Default.aspx page. Paste following code in your Default.aspx page.

 

Your Name: 

 


Add following code in your button click event.

 

  protected void btnSendQueryString_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default2.aspx?name=" + txtQueryStringVal.Text.Trim());
    }

 

 

Step3: Add new webpage to get query string value and display on page.

I have created Default2.aspx page. Paste following code in your default2.aspx page.

 


 

Paste following code in your Default2.aspx.cs page load method.

 

 

 //--- To check if query string has value or not
        if (!string.IsNullOrEmpty(Request.QueryString.ToString()))
        {
            //--- We can get value of query string by passing name of query string or by passing its index
            string a = Request.QueryString["name"].ToString();
            string b = Request.QueryString[0].ToString();

            lblQueryStringResult.Text = a.ToString();
        }

 

 

Final Output:


Query String example in asp .net

 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice