The Query string is a common way of passing data or "Query Parameter" to a web page. The web page can receive this parameter and react accordingly. A Query string can be a search keyword or a page number or something similar. Query string values are appended to the end of the web page URL. They start with a question mark (?) followed by the query string term (or parameter name) followed by an equal sign (=) and the given parameter’s value. You can add as many query parameters as you like. However the next parameter will be delimited with an ampersand (&). Now lets dive into the code to under stand it much better
Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ClientQueryString._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
<tr>
<td>
<asp:HyperLink ID="Hyperlink1" runat="server">Count Query Clicksasp:HyperLink>
td>
<td>
<asp:Label ID="lbldisplaymessage1" runat="server" >asp:Label>
td>
tr>
table>
In the code behind file
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ClientQueryString
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//read the query string
int queryClicks;
if (Request.QueryString["clicks"] != null)
{
queryClicks = int.Parse(Request.QueryString["clicks"]) + 1;
}
else
{
queryClicks = 1;
}
//define the query string in the hyperlink
Hyperlink1.NavigateUrl += "?clicks=" + queryClicks.ToString();
lbldisplaymessage1.Text = "Query clicks: " + queryClicks.ToString();
}
}
}
}
The output screen shot of the above program
