0

I'm trying to pass a text to my JavaScript function as bellow.

hplDetails.NavigateUrl = "JavaScript:GetSpecialEquipmentsDetails('" + ((SGViagens.Entities.SpecialEquipment)(e.Item.DataItem)).EquipmentCode + "','" + ((SGViagens.Entities.SpecialEquipment)(e.Item.DataItem)).EquipmentName + "')";

My second parameter contains any text with accents, special characters. But when I get in my JavaScript function, the text is mangled. Anyone have any tips for me?

2
  • 2
    Could you show what the text looks like in C# and in JS?
    – Tetaxa
    Oct 17, 2011 at 13:08
  • in C# is 'Assento para Bebês', in JS show me Assentos%20para%20beb%C3%83....
    – mcamara
    Oct 17, 2011 at 13:13

3 Answers 3

1

Instead of <asp:HyperLink> try having such thing:

<a id="hplDetails" runat="server">Text here</a>

Then assign its URL with such code:

hplDetails.Attributes["href"] = "URL here.....";

Hopefully this won't mess your special characters.

1

ASP.NET is encoding the NavigateUrl property.

Use decodeURI in your js function.

2
  • Now it's true, but the accents are still having problems as 'Assento para bebês
    – mcamara
    Oct 17, 2011 at 13:24
  • @tomcamara - maybe try unescape? Oct 17, 2011 at 13:42
1

Having a utility function such as this in a class called, say StringUtil:

public static string JsEncode(string text)
{
    StringBuilder safe = new StringBuilder();
    foreach (char ch in text)
    {
        // Hex encode "\xFF"
        if (ch <= 127)
            safe.Append("\\x" + ((int)ch).ToString("x2"));
        // Unicode hex encode "\uFFFF"
        else
            safe.Append("\\u" + ((int)ch).ToString("x4"));
    }
    return safe.ToString();
}

... mean you can then encode the values as safe, JavaScript-encoded strings:

hplDetails.NavigateUrl = "JavaScript:GetSpecialEquipmentsDetails('" + StringUtil.JSEncode( ((SGViagens.Entities.SpecialEquipment)(e.Item.DataItem)).EquipmentCode + "','" + ((SGViagens.Entities.SpecialEquipment)(e.Item.DataItem)).EquipmentName ) + "')";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.