Home Asp.net Pass Multiple Parameters in Asp.net QueryString Example

Pass Multiple Parameters in Asp.net QueryString Example

9
1

In in this tutorial, I’ll explain as to how you can pass multiple parameters in asp.net querystring url and get parameter values on page load event of another page in asp.net using c# as well as vb.net. I would also demonstrate an example code and step by step guide.

Just for refreshing, you can also check out my previous tutorials, in which I had explained how to pass & (ampersand) as a querystring parameter value, how to get querystring paramters value using jquery, and exporting gridview data to word excel text and pdf.  I have also posted tutorials on GridView, Asp.net.

 

The QueryString collection is used to retrieve the variable values in the HTTP query string. The HTTP query string is specified by the values following the question mark (?).

Simple Asp.net QueryString URL Example With Single Parameter

protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Redirect(“Page2.aspx?id=” + txtId.Text);
}

As you can see from the above query string example (just after “?”), we are passing the parameters in key:value pair, meaning that id is a key variable and txtId.Text is a value of that key.

Note: To pass more than one parameter within the query string url, we need to use & (ampersand) to separate the parameters. Check below for the complete example.

For this tutorial, I created two pages, Page1.aspx is used to redirect the user to another page with a query string and another page is Page2.aspx to get the parsed query string data and display on a webpage.

Pass Multiple Parameters in Asp.net QueryString – [Page1.aspx]

Add the following code to your Page1.aspx Page as follows:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>Set QueryString Data From Code-behind</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<h4>Page1.aspx: Enter Employee Details</h4>
<table>
<tr>
<td>Employee Id:</td>
<td><asp:TextBox ID=”txtId” runat=”server” /></td>
</tr>
<tr>
<td>Employee Name:</td>
<td><asp:TextBox ID=”txtName” runat=”server” /></td>
</tr>
<tr>
<td>Designation:</td>
<td><asp:TextBox ID=”txtDesig” runat=”server” /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<asp:Button ID=”btnSubmit” runat=”server” Text=”Submit” OnClick=”btnSubmit_Click” />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

As you can see from the above code, I used OnClick event from where we will redirect users to another page with query string. Now add the following code snippet to your code-behind file of Page1.aspx as below.

Pass Multiple Parameters in Asp.net QueryString – [C#/Vb.net]

If you are using C# as code-behind, use the following code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
Response.Redirect(“Page2.aspx?id=” + txtId.Text + “&name=” + txtName.Text
+ “&desig=” + txtDesig.Text);
}

If you are using Vb.net as code-behind, then use the following code:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
Response.Redirect(“Page2.aspx?id=” & txtId.Text & “&name=” & txtName.Text
& “&desig=” & txtDesig.Text)
End Sub

To get the query string values to another page, we need to use the following code to the page that we mentioned in query string url. I used Page2.aspx as example.

Get QueryString URL Parameter Values To Another Page – [Page2.aspx]

Add the following code to your Page2.aspx Page as follows:

<html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>Get QueryString Data From Another Page in Asp.net</title>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<h4>Page2.aspx: Get Employee Details From Previous Page QueryString</h4>
<b>Employee Id:</b>
<asp:Label ID=”lblId” runat=”server” />
<br />
<b>Employee Name:</b>
<asp:Label ID=”lblName” runat=”server” />
<br />
<b>Designation:</b>
<asp:Label ID=”lblDesig” runat=”server” />
</div>
</form>
</body>
</html>

Add the following code snippet to your code-behind file of Page2.aspx as below.

Get QueryString URL Parameter Values To Another Page – [C#/Vb.net]

If you are using C# as code-behind, use the following code:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lblId.Text = Request.QueryString[“id”];
lblName.Text = Request.QueryString[“name”];
lblDesig.Text = Request.QueryString[“desig”];
}
}

If you are using Vb.net as code-behind, use the following code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
lblId.Text = Request.QueryString(“id”)
lblName.Text = Request.QueryString(“name”)
lblDesig.Text = Request.QueryString(“desig”)
End If
End Sub

How to pass &(ampersand) within QueryString URL?

How to Get QueryString Parameter Value using JavaScript/jQuery?

Example Result

When you click on the submit button, you will be redirected to Page2.aspx with the following query string:

http://localhost:4426/Page2.aspx?id=EMP0001&name=Mack%20John&desig=Managing%20Director

And here is the result generated from the above example:

Asp.net QueryString URL Example to Pass Multiple Parameters using C# Vb.net

Download Example

[wpdm_file id=33]

Git Repo

1 COMMENT

  1. This was exactly the solution I was looking for! Thank you so much. I have spent the better part of 2 days trying to get my gridviews to handle filtering criteria correctly – and I just felt like I was going backwards. This solution worked perfectly! 🙂 I really appreciate it!

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!