Home Asp.net Asp.net Passing &(ampersand) in QueryString Parameter Value

Asp.net Passing &(ampersand) in QueryString Parameter Value

136
0
Redirect Page on Button Click

Now here in this tutorial, I’ll explain how to proceed with passing & (ampersand) in querystring parameter value in asp.net using c#, vb.net as well as javascript/jquery with example code and step by step guide.

In my previous tutorials, I’d explained passing datatable as storedprocedure parameter in asp.net, get gridview selected row hiddenfield value using jquery, exporting gridview data to word excel text and pdf and other more cracking tutorials on GridView, Asp.net here.

I already explained how to pass querystring with multiple parameters in asp.net here.

As you know that passing &(ampersand) in querystring is not allowed because ampersand is used as a separator to pass multiple parameters within query string url. Its OK when your query string url doesn’t contains & (ampersand) in a query string parameter value but what happened if you are passing &(ampersand) in querystring?

It cut off the contains value after the & (ampersand) of a specific key variable. For Example, If you pass the value like desig=R&D Expert then the value for the design will be shown as R instead R&D Expert.

To resolve above error, we need to use Server.UrlEncode() method as following example.

Passing &(ampersand) in QueryString Using C# or Vb.net – [.cs/.vb]

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

Response.Redirect("Page2.aspx?id=" + txtId.Text + "&name=" + txtName.Text + "&desig=" + Server.UrlEncode(txtDesig.Text));

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

Response.Redirect("Page2.aspx?id=" & txtId.Text & "&name=" & txtName.Text & "&desig=" & Server.UrlEncode(txtDesig.Text))

As you can see from the above example, I used Server.UrlEncode() method to encode the & (ampersand) within query string value for both c# and vb.net example.

If you want to use JavaScript/jQuery, use the following code:

<script type="text/javascript">
    function callQueryString() {
        var url = "http://localhost:2048/Page2.aspx/?id=EMP001&desig=R%26D Expert";
        window.location.href = url;
    }
</script>

As you can see from the above example, I encoded & (ampersand) as %26 to escape the & (ampersand) within query string parameter value.

How to pass Multiple Parameters within QueryString?

You can learn more about how pass multiple parameters within a querystring url here.

How to Get QueryString Parameter Value using JavaScript/jQuery?

You can learn more about how to get querystring parameter value from client-side using jquery here.

Previous articlePass Multiple Parameters in Asp.net QueryString Example
Next articleGet QueryString Value in jQuery from Client-side in Asp.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

three × 1 =