Title
Query String In ASP.Net
Introduction
In the World Wide Web, a query string is the part of a uniform resource locator (URL) containing data that does not fit conveniently into a hierarchical path structure. The query string commonly includes fields added to a base URI by a Web browser or other client application, for example as part of an HTML form.
In simple words, we can say Query String is method of passing values from one page to another. For example while you Google something, you might have observed weird internet address, as shown below.
http://localhost:30203/query_string2.aspx?x=Paras&y=Babbar
This html addresses use QueryString
property to pass values between pages. In this address you send 3 information.
- query_string2.aspx, this is the page your browser will go
- x=Paras. you send ‘x’ variable whose value is set to Paras
- y=Babbar, you send ‘y’ variable whose value is set to Babbar
Now see below code.
Query_String1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="query_string1.aspx.cs" Inherits="query_string1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2>Page 1</h2> <table> <tr><th>First Name:</th> <td> <asp:TextBox ID="txt_fname" runat="server"></asp:TextBox> </td> </tr> <tr> <th>Last Name:</th> <td><asp:TextBox ID="txt_lname" runat="server"></asp:TextBox></td> </tr> <tr> <td colspan="2"> <asp:Button ID="btn_click" runat="server" Text="Click" OnClick="btn_click_Click" /> </td> </tr> </table> </div> </form> </body> </html>
Query_String1.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class query_string1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btn_click_Click(object sender, EventArgs e) { var fnm = txt_fname.Text; var lnm = txt_lname.Text; Response.Redirect("query_string2.aspx?x=" + fnm + "&y=" + lnm); } }
Here,
var fnm = txt_fname.Text; var lnm = txt_lname.Text; Response.Redirect("query_string2.aspx?x=" + fnm + "&y=" + lnm);
we’ve store value of two text box in two variable and pass it to query_string2.aspx page using Response.Redirect.
Output
Now let see what happens at query_string2.aspx.
query_string2.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="query_string2.aspx.cs" Inherits="query_string_2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <h2>Page 2</h2> <table> <tr> <th> First Name: </th> <td> <asp:Label ID="lbl_fname" runat="server"></asp:Label> </td> </tr> <tr> <th> Last Name: </th> <td> <asp:Label ID="lbl_lname" runat="server"></asp:Label> </td> </tr> </table> </div> </form> </body> </html>
query_string2.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class query_string_2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var a = Request.QueryString["x"]; var b = Request.QueryString["y"]; lbl_fname.Text = a; lbl_lname.Text = b; } }
Here we are requesting the query string variables using Request.QueryString method. And then values are printed in label control.
Output