Query String In ASP.NET
Query string is client side state management variable. This is very easy to use. Basically we are using query string to send small amount of text from one page to another with help of page url. Just after page url you have to add ? sign and after that add your query string key and value. Whatever data you send through query string that visible to everyone.
Let’s see how to send and retrieve query string value.
Below codes is redirecting to Default4.aspx page and added a query string “Name” that value is Amit.
// Send Query String
Response.Redirect("Default4.aspx?Name=Amit");
On Default4.aspx, first check that query string is available or not. If available then retrieve that query string value using query string name.
// Retrieve Query String Value
if (Request.QueryString["Name"] != null)
{
string name = Request.QueryString["Name"].ToString();
Send more than one query string
Query string allow user to send multiple query string. In below example, we are sending two query string Name and City to Default4.aspx. It’s very easy to send more than one query string, you need to add & sign just after first query string.
// Send Query String
Response.Redirect("Default4.aspx?Name=Amit&City=Buxar");
On Default4.aspx, you retrieve all query string by using their names.
// Retrieve Query String Value
if (Request.QueryString != null)
{
string name = Request.QueryString["Name"].ToString();
string city = Request.QueryString["City"].ToString();
You can also retrieve query string values by using index instead of name. First query string at zero index and after so on.
// Retrieve Query String Value By Using Index
if (Request.QueryString != null)
{
string name = Request.QueryString[0].ToString();
string city = Request.QueryString[1].ToString();
}
Handle multiple query string
In some cases, you have to send multiple query string that every times not fixed to how many query string you have to send. For example suppose sometimes you have to send 4 query string sometimes 5 query string.
// Send Query String
Response.Redirect("Default4.aspx?Name1=Amit&Name2=Sharad&Name3=Sonu&Name4=Sanjiv");
When you are not confirm about numbers of query string, then just use for loop to retrieve all query by using their index.
// Retrieve Query String Value
if (Request.QueryString != null)
{
for (int i = 0; i < Request.QueryString.Count; i++)
{
Response.Write(Request.QueryString[i]);
}
// Output - Amit Sharad Sonu Sanjiv
Use encoded query string
We learn that query string is very easy to use but apart from using it have some disadvantage that query string cannot accept Space and & characters. When you send space in query string you will see in url that space is replaced to %20 and whenever you send any query string that value contains & character then while retrieving it show only that text that comes before & sign. For example, if you are sending query string Name that value is Tom&Jerry then on other side it will retrieve as Tom only.
For overcoming these type of problems, use Server.UrlEncode method that encode your query string value and you can retrieve that value without decoded.
In below code examples, we are adding two TextBoxes, so that you can enter your name and city and on click of Send Button your query string send to another page using Server.UrlEncode method.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Query String Send</title>
</head>
<body style="margin-left:100px;">
<form id="form1" runat="server">
<table width="280px">
<tr>
<td>Name</td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>City</td>
<td>
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2" align="Center">
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
Below codes are showing to how to use Server.UrlEncode method on Send Button's click.
protected void btnSend_Click(object sender, EventArgs e)
{
// Send Query String
Response.Redirect("Default4.aspx?Name=" + Server.UrlEncode(txtName.Text) +
"&City=" + Server.UrlEncode(txtCity.Text));
}
You can retrive encoded query string without decoded.
protected void Page_Load(object sender, EventArgs e)
{
// Retrieve Query String Value
if (Request.QueryString != null)
{
string name = Request.QueryString["Name"].ToString();
string city = Request.QueryString["City"].ToString();
}
Below demonstrating query string that:
1. how you can simply use query string to send data
2. What is problem, when you are sending & character in your query string value.
3. What is benefit of using Server.UrlEncode method.