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? Commented Oct 17, 2011 at 13:08
  • in C# is 'Assento para Bebês', in JS show me Assentos%20para%20beb%C3%83.... Commented 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.

Sign up to request clarification or add additional context in comments.

Comments

1

ASP.NET is encoding the NavigateUrl property.

Use decodeURI in your js function.

2 Comments

Now it's true, but the accents are still having problems as 'Assento para bebês
@tomcamara - maybe try unescape?
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 ) + "')";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.