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:
- 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.
- www.csharpcorner.com/userdetail.aspx?useid=1
It displays the userdetail which userid = 1
Advantages and Disadvantages of Query String
Advantages of Query String
- Very Light weight state management.
- No server side resources at all.
- Fast and easily pass value from one to another page.
Disadvantages of Query String
- Value is visible in URL.
- No security at all.
- 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
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <h3>This is Home page with search control.<br /> User select search by and typed the value.
- <Br />After click on button redirect the user to search result page</h3>
- <p> </p> Search By :
- <asp:DropDownList ID="ddlSearchBy" runat="server">
- <asp:ListItem>Member Name</asp:ListItem>
- <asp:ListItem>Membership Type</asp:ListItem>
- <asp:ListItem>Member City</asp:ListItem>
- <asp:ListItem>Member State</asp:ListItem>
- </asp:DropDownList> =
- <asp:TextBox ID="txtSearchValue" runat="server"></asp:TextBox> <br />
- <asp:Button ID="btnSearchResult" runat="server" Text="Search Result" OnClick="btnSearchResult_Click" /> </div>
- </form>
- </body>
-
- </html>
Default.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class _Default: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {}
- protected void btnSearchResult_Click(object sender, EventArgs e) {
- string searchby = null;
- searchby = ddlSearchBy.SelectedValue;
-
- Response.Redirect("SearchResult.aspx?searchby=" + searchby + "&searchvalue=" + txtSearchValue.Text);
- }
- }
SearchResult.aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SearchResult.aspx.cs" Inherits="SearchResult" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div> Total Query String Variable Count:
- <asp:Label ID="lblQueryStringVariableCount" runat="server"></asp:Label> <br /> <br /> Search By :
- <asp:Label ID="lblSearchBy" runat="server" Text="Label"></asp:Label> <br /> <br /> Search Value :
- <asp:Label ID="lblSearchValue" runat="server" Text="Label"></asp:Label>
- </div>
- </form>
- </body>
-
- </html>
SearchResult.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- public partial class SearchResult: System.Web.UI.Page {
- protected void Page_Load(object sender, EventArgs e) {
-
- int TotalVariable = Request.QueryString.Count;
- lblQueryStringVariableCount.Text = Convert.ToString(TotalVariable);
- string searchBy = Request.QueryString["searchby"].ToString();
- string searchValue = Request.QueryString["searchvalue"].ToString();
-
- lblSearchBy.Text = searchBy;
- lblSearchValue.Text = searchValue;
- }
- }
Output
Default.aspx
SearchResult.aspx