Written by Ananya Chandra on February 9, 2014 in C#

Query String in ASP.NET [Understanding the Concept]

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this article, we are going to check out what is query string in asp.net and how we can use it. We’ve been bringing many programming tutorials, and you might be interested in checking out our previous articles like View State in ASP.Net and in this post we are going to check out about Query String in ASP.Net in a closer look.

Query string is a client side state management technique. Query string is used to store the information between multiple page visits. By creating a new URL for every fresh page it keeps track of the number of visits made to the page.

The following example will give you a brief idea on how the query string works. There are two .aspx pages, in Default1.aspx 

<form id="form1" runat="server">

<div>

This is page 1.

<asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl="~/Default2.aspx">Page2</asp:HyperLink>

</div>

</form>

Similarly in Default2.aspx

<form id="form1" runat="server">

<div>

This is page 2.

<asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl="~/Default1.aspx">Page1</asp:HyperLink>

</div>

</form>

Basically by using hyperlink we can navigate between the pages. Each time we click on the hyper link to navigate the query string is incremented by 1 and can be seen in the URL.

In Default1.aspx.cs page

int count;

protected void Page_Load(object sender, EventArgs e)

{

if (Request.QueryString["count"] == null)

{

HyperLink1.NavigateUrl = "~/Default2.aspx?count=" + count.ToString(); // condition true, navigate to next page

// condition true, navigate to next page

}

else

{

count = Convert.ToInt32(Request.QueryString["count"].ToString()) + 1; // condition false, increment count value by 1

// condition false, increment count value by 1

HyperLink1.NavigateUrl = "~/Default2.aspx?count=" + count.ToString();

}

Similarly in Default2.aspx.cs page

int count;

protected void Page_Load(object sender, EventArgs e)

{

if (Request.QueryString["count"] == null)

{

HyperLink1.NavigateUrl = "~/Default1.aspx?count=" + count.ToString(); // condition true, navigate to next page

// condition true, navigate to next page

}

else

{

count = Convert.ToInt32(Request.QueryString["count"].ToString()) + 1;

// condition false, increment count value by 1

HyperLink1.NavigateUrl = "~/Default1.aspx?count=" + count.ToString(); // condition false, increment count value by 1

}

Output:

The highlighted indicates that this is page 1, after clicking on the hyperlink you will be redirected to page 2

query string in asp.net output1

This is page 2 and the count is “0” , after clicking on the hyperlink you will be redirected back to page 1

query string in asp.net output2

This is page 1 and the count is incremented to “1”

query string in asp.net output3

The count in the URL signifies the number of times you have visited the page.

Hope you enjoyed our article on how Query String in ASP.Net works and we’ll be coming up with more tutorials. Stay tuned.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: