Sunday, 27 August 2017

How to Pass Ampersand (&) as QueryString parameter value in Asp.Net?

In this post, I will explain How to Pass Ampersand (&) as QueryString parameter value in Asp.Net?

In many cases, we need to pass value of variable from one page to another page using QueryString. It generally works fine but when a parameter value contains Ampersand (&) symbol then another page reads wrong parameter value. For example, if suppose Parameter value is "Larsen & Turbo" then another page will read it as "Larsen ".

This happens because generally, Ampersand (&) is used as a separator between 2 parameters in QueryString. You can see the following example,

www.programmingtrends.com?Param1=Google&Param2=India

To solve this problem with An ampersand (&) we can use Server.UrlEncode() function to encode Ampersand (&) in parameter value or we can encode parameter value by replacing '&' with '%26'.

Example:
string Param="Larsen & Turbo";

Response.Redirect("www.programmingtrends.com?param1=" + Server.UrlEncode( Param ));

OR

Response.Redirect("www.programmingtrends.com?param1=" + Param.Replace("&", "%26"));

0 comments:

Post a Comment

Please do not enter any spam link in the message box.