QueryString in ASP.Net

Mohammed Imtiyaz Feb 22, 2015

Demo

QueryString in ASP.Net

A QueryString is a variable specified in HTML query that can be accessed easily with ASP.Net. The QueryString is appended at the end of the URL (Uniform Resource Locator) followed by a question mark ('?'). We can specify multiple QueryString in the URL separated by ampersand ('&') or a semicolon (';')

QueryString can be generated by several different ways. For example, the following anchor tags generates a variable named "data" with the value "helloworld".

<a href="Default.aspx?data=helloworld">Single Parameter</a>

In case if we need to send multiple parameters to another page we need to write the code as:

<a href="Default2.aspx?empId=100&empName=Imtiyaz">multiple Parameters</a>

The example below will demonstrate QueryString in ASP.Net

<table border="1" width="350px">
    <tr>
        <td style="width: 100%" colspan="2">
            <span>QueryString in ASP.Net</span>
        </td>
    </tr>
    <tr>
        <td style="width: 40%">Employee Id</td>
        <td style="width: 60%">
            <asp:TextBox ID="txtEmployeeId" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td style="width: 40%">Employee Name</td>
        <td style="width: 60%">
            <asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td style="width: 100%" colspan="2">
            <asp:Button ID="btnSend" runat="server" Text="Send data from codeBehind" OnClick="btnSend_Click" />
      	</td>
    </tr>
</table>

C#

Send data from Default.aspx page to Default2.aspx page from codeBehind.

protected void btnSend_Click(object sender, EventArgs e)
{
    Response.Redirect("Default2.aspx?empId=" + txtEmployeeId.Text + "&empName=" + txtEmployeeName.Text + "");
}

Now retrieve the data at Default2.aspx.cs file at page load event using the following code.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // First Method
        if (Request.QueryString["empId"].ToString() != "")
        {
            lblEmployeeId.Text = Request.QueryString["empId"].ToString();
            lblEmployeeName.Text = Request.QueryString["empName"].ToString();
        }
      	else
        {
            lblEmployeeId.Text = "-";
            lblEmployeeName.Text = "-";
        }
      
        // Second Method
        if (Request.QueryString["empId"].ToString() != "")
        {
            lblEmployeeId.Text = Request.QueryString[0].ToString();
            lblEmployeeName.Text = Request.QueryString[1].ToString();
        } 
     	else
        {
            lblEmployeeId.Text = "-";
            lblEmployeeName.Text = "-";
        }
    }
}

To retrieve the data at FrontEnd (Default2.aspx) without using codeBehind, use the following:

<!DOCTYPE html>
<html>
   <head>
      <title>HTML marquee Tag</title>
   </head>
   <body>
	Employee Id : <%=Request.QueryString["empId"] %>
	Employee Name: <%=Request.QueryString["empName"] %>
   </body>
</html>