2

I am currently having troubles figuring out how to handle a filepath to be (dynamicly) passed out to a HyperLink control's NavigateUrl property.

Let's say that I'm trying to refer to a file named jäynä.txt at the root of C:.
Passing "file:///C:/jäynä.txt" result to a link to file:///C:/jäynä.txt, as does HttpUtility.UrlPathEncode("file:///C:/jäynä.txt").

Replacing the ä**s with **%E4, which gives the string "file:///C:/j%E4yn%E4.txt", does give a working link to file:///C:/jäynä.txt, but I have not been able to find a way to make the replacement without defining it myself. With Replace("ä", "%E4"), for example.

Is there a way to automaticly handle the filepath string so that the HyperLink would display it correctly, without manualy listing what characters to replace in the string?

Additional Note:
There may be a way to work around this by spesifying the character encoding in which the page is rendered, because debugging shows that the HyperLink at least saves the string "file:///C:/jäynä.txt" unchanged, but somehow mangles it around the time of rendering.
However, this seems only be the case in rendering of the NavigateUrl because other components as well as HyperLink's Text-property are all quite capable of rendering the character ä unchanged.

vipirtti
  • 1,058
  • 4
  • 15
  • 24

4 Answers4

4

The NavigateUrl property of a Hyperlink will encode unicode chars in the url.

Instead you can set the href attribute property of the Hyperlink like this:

hyperlink1.Attribute("href") = "file:///C:/jäynä.txt"
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
1

Don't use HyperLink control. Instead use HtmlAnchor control. It will solve your problem. I don't know why Microsoft designed like this.

Syed
  • 21
  • 3
1

This is due to how the browser starts to interpret the path, typically individuals will avoid using characters such as that in the urls of pages.

In your case, I believe you have struck upon the best case scenario, as I am not aware of any way to change the behavior of HttpUtility and/or the NavigateUrl property. At least not without creating a custom control for it.

Mitchel Sellers
  • 62,228
  • 14
  • 110
  • 173
  • No, I do not belive it is the browser. I just checked the source of the page in the first case and the link href is href="file:///C:/j%c3%a4yn%c3%a4.txt", suggesting it's how the server has rendered it in the first place. You may be right on the sollution tough. – vipirtti Mar 09 '09 at 13:52
  • It might indirectly be the browser, based on something in BrowserCaps or something similar to render it to the system. Regardless, the values that you specify to the NavigateUrl must be pre-cleaned. That is documented as it does not automatically do anything with UrlEncode. – Mitchel Sellers Mar 09 '09 at 13:59
0

Thank you! The post using the 'attributes' solved my problem. In my case it was

HyperLink6.Attributes["href"] = "http://høgstedt.danquah.dk/";

The problem of using special danish characters in a url seem to have been troubling a lot of programmers - a search provides several very complicated approaches. This one is SIMPLE and it SIMPLY WORKS. So once again, thank you

  • 1
    Don't post an answer only to say thank you. Just a comment is enough. And an upvote is even more appreciated – Steve Dec 19 '12 at 09:30