0

I want to go to a different URL and pass a parameter as a query string. This value contains an ampersand. I can use Response.Redirect to do this.

Response.Redirect("http://www.mysite.com/?Value=" + Server.UrlEncode("This & That"))

However, I prefer to use the PostBackUrl property of the LinkButton control to do this because I want to pass extra information in addition to the value in the URL. If I try

<asp:LinkButton id="ID" runat="server" PostBackUrl="http://www.mysite.com?Value=This+%26+That"/>

and click on the link in IE, then I see http://www.mysite.com?Value=This+%26+That as the new URL and can use Request.QueryString["Value"] to obtain the value. That works fine.

However, if I use Chrome or Firefox, then the new URL is http://www.mysite.com?Value=This+&+That, and I get "This " as the value instead of "This & That". It appears that different JavaScript is generated in IE and other browsers on the page containing the LinkButton. What can I do to get around the problem? Why the difference between IE and other browsers?

If I use the NavigateUrl property of a HyperLink control instead, then I get the correct URL in all the browsers that I tried.

mbnovick
  • 1
  • 1
  • 2
  • Did you check by clicking in FF and Chrome? Chances are that this is just how the links _display_ on the hover preview. – Oded Jan 09 '12 at 11:24
  • why do you want to use that URL....simply use `http://www.mysite.com?Value=This+&+That`. It also works – Pankaj Upadhyay Jan 09 '12 at 11:25
  • @Oded I checked what link appeared in both FF and Chrome after I clicked on the link and went to the next page. Moreover, I looked at Request.QueryString["Value"] on the new page and saw that it returned "This " when I used these browsers, but it returned "This & That" for IE. This is not surprising. HTML for IE was my text. – mbnovick Jan 10 '12 at 09:40
  • @Oded For FF and Chrome the HTML was my text The difference between the two is that HTML for IE contains %2526 where FF and Chrome contain %26. – mbnovick Jan 10 '12 at 09:44
  • @Pankaj In the original question I mentioned that does not work because Request.QueryString["Value"] is "This " for the resulting URL, so I can't get the correct value by using Request.QueryString. – mbnovick Jan 10 '12 at 09:52

2 Answers2

0

I just came across the same problem, I solved it by double encoding the value. Upon retrieving you have to decode it manually (for the second time, because ASP does it already once automatically for you).

To double-encode the value:

Response.Redirect("http://www.mysite.com/?Value=" + Server.UrlEncode(Server.UrlEncode("This & That")));

To get the original value:

String value = Server.UrlDecode(Request.QueryString["Value"]);
0

u can use

protected void LinkButton1_Click(object sender, EventArgs e)
{
  string qs = HttpUtility.UrlEncode("This & That", System.Text.Encoding.Default);
  Response.Redirect("http://www.mysite.com?Value="+qs);
}
reshma k
  • 544
  • 1
  • 3
  • 11
  • This answer works, but it's basically the same as using Server.UrlEncode, which I suggested in the original question. That submits a Get request rather than a Post request, so it does not directly allow passing extra information to the mysite.com URL. It also performs an extra Redirect, so using HyperLink with NavigateUrl would be better, since it avoids the Redirect. – mbnovick Jan 10 '12 at 10:09