Query String In ASP.NET

You will learn about the following concepts:

  • What is Query String?
  • What are the uses of Query String and Types?
  • Real world example.
  • Advantages and disadvantages of cookies
  • Maximum limit of Query String.
  • Sample code using Query String will answer the following:
  • How to generate a query string?
  • How to count the number of variables in query string?
  • How to use it in query?

Query String

Query string is a family member of state management. This works on the client side in the browser. In this we send the key and value type of string in URL. Target page will receive this string and act on the query string.

Basically we need the query string to transfer certain information from one page to another. All the data in query string is visible in URL.

Uses of Query String

In web applications in  ASP.NET we redirect/transfer the user from one page to another usually, redirecting the user with some useful information data or ID value. That data transferring or passing with key and value pattern within the URL is called Query String.

Single Value Query String

In single value query string we pass the one variable's name and one value of that variable in URL.

Syntax

www.sitename.com/<page name>?variableName=value

Example

http://www.c-sharpcorner.com/article.aspx?author=manojkalla

Multiple Value Query String

In multiple value query strings we pass more than one variable and its value in URL. & ampersand distinguishes the query string variables.

Syntax

www.sitename.com/<page name>?variableName1=value1&variableName2=value2

Example

http://www.c-sharpcorner.com/myaccount/AuthorCreatedArticleDetails.aspx?Path=registration&iscomplete=true

In the above example you can see there are two variables,

“Path” is variable and its value is “registration” and second variable is “iscomplete” and its value is “true”.

Variable Name Value
Path registration
iscomplete true

Real World examples

Query string is mostly used in the following scenarios:

  1. www.csharpcorner.com/orderdetail.aspx?order=121&foodtype=JainVeg
    Above url redirects the user on orderdetail.aspx page and passes the two variables where order id = 121 and food type = jainveg.

  2. www.csharpcorner.com/userdetail.aspx?useid=1
    It displays the userdetail which userid = 1

Advantages and Disadvantages of Query String

Advantages of Query String

  1. Very Light weight state management.
  2. No server side resources at all.
  3. Fast and easily pass value from one to another page.

Disadvantages of Query String

  1. Value is visible in URL.
  2. No security at all.
  3. Limitation of size differs from browser to browser.

Maximum limit of Query String

In web.config file you can set the size setting of query string. Also the size of query string differs from browser to browser.

For more information on this please refer to this link,

  • http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string
  • https://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.maxquerystringlength(v=vs.110).aspx
Sample code of using cookies

Create a new empty web site project named “QueryStringExample”



Right click on project and select Add-->Add New Item and select WebForm



Add a new Web Form named “Default.aspx”, in this page it will accept user id and password as well as create cookies.



In default.aspx page we are planning for search. Suppose this is my default home page where user can select search by and type search value, on button click it redirects to searchresult.aspx page.

Server Control Type Control ID Description
Dropdown List ddlSearchBy Dropdown list to display search by options.
Search By Options
Member Name
Membership Type
Member City
Member State
TextBox txtSearchValue Textbox for feeding the search value.
Button btnSearchResult Button to submit the request to server.

Right click on project and select Add-->Add New Item and select WebForm



Add a new Web Form named “SearchResult.aspx”, in this page



In searchresult.aspx page user will land with query string to know the search result.

Example

www.sitename.com/searchresult.aspx?searchby=member name&searchvalue=beniwal

Server Control Type Control ID Description
Label lblSearchBy To display search by selected category.
Label lblSearchValue To display entered search value.
Label lblQueryStringVariableCount To display numbers of variable of query strings.

From this page query string will generate like this,

http://localhost:57288/SearchResult.aspx?searchby=Member%20Name&searchvalue=Beniwal

Default.aspx code

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div>  
  12.                 <h3>This is Home page with search control.<br /> User select search by and typed the value.  
  13.                     <Br />After click on button redirect the user to search result page</h3>  
  14.                 <p> </p> Search By :   
  15.                 <asp:DropDownList ID="ddlSearchBy" runat="server">  
  16.                     <asp:ListItem>Member Name</asp:ListItem>  
  17.                     <asp:ListItem>Membership Type</asp:ListItem>  
  18.                     <asp:ListItem>Member City</asp:ListItem>  
  19.                     <asp:ListItem>Member State</asp:ListItem>  
  20.                 </asp:DropDownList>     =   
  21.                 <asp:TextBox ID="txtSearchValue" runat="server"></asp:TextBox> <br />  
  22.                 <asp:Button ID="btnSearchResult" runat="server" Text="Search Result" OnClick="btnSearchResult_Click" /> </div>  
  23.         </form>  
  24.     </body>  
  25.   
  26.     </html>  
Default.aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class _Default: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {}  
  9.     protected void btnSearchResult_Click(object sender, EventArgs e) {  
  10.         string searchby = null;  
  11.         searchby = ddlSearchBy.SelectedValue;  
  12.         //Generating Query string for page called SearchResult.aspx  
  13.         Response.Redirect("SearchResult.aspx?searchby=" + searchby + "&searchvalue=" + txtSearchValue.Text);  
  14.     }  
  15. }  
SearchResult.aspx code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResult.aspx.cs" Inherits="SearchResult" %>  
  2.     <!DOCTYPE html>  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head runat="server">  
  6.         <title></title>  
  7.     </head>  
  8.   
  9.     <body>  
  10.         <form id="form1" runat="server">  
  11.             <div> Total Query String Variable Count:      
  12.                 <asp:Label ID="lblQueryStringVariableCount" runat="server"></asp:Label> <br /> <br /> Search By :      
  13.                 <asp:Label ID="lblSearchBy" runat="server" Text="Label"></asp:Label> <br /> <br /> Search Value :      
  14.                 <asp:Label ID="lblSearchValue" runat="server" Text="Label"></asp:Label>  
  15.             </div>  
  16.         </form>  
  17.     </body>  
  18.   
  19.     </html>  
SearchResult.aspx.cs code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. public partial class SearchResult: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {  
  9.         //Total Count of Query string variable   
  10.         int TotalVariable = Request.QueryString.Count;  
  11.         lblQueryStringVariableCount.Text = Convert.ToString(TotalVariable);  
  12.         string searchBy = Request.QueryString["searchby"].ToString();  
  13.         string searchValue = Request.QueryString["searchvalue"].ToString();  
  14.         //Now you can this variable value in query or for other kind of things.  
  15.         lblSearchBy.Text = searchBy;  
  16.         lblSearchValue.Text = searchValue;  
  17.     }  
  18. }  
Output

Default.aspx


SearchResult.aspx

Ebook Download
View all
Learn
View all