<asp:HyperLink ID="TestHyperLink" runat="server"></asp:HyperLink>
I am having the above hyperlink. I am just setting the text with below code:
string textFromUser = "This is test's.";
string encodedText=HttpUtility.HtmlEncode(textFromUser);
TestHyperLink.Text = encodedText;
TestHyperLink.ToolTip = encodedText;
The issue is that text of hyperlink is coming correct but tooltip is showing encoded characters.
How can I make tooltip appear in the same way as text is?
You're seeing that problem because the attribute value is being encoded twice.
'
character is encoded into '
;&
character is encoded into &
.Finally the output is sent to the browser as title="This is test&quot;s."
. Since the attribute value is already being encoded by default, you can safely set the Tooltip
property to the raw text and encode only the text for the Text
property.
Note: In this case the attribute encoding is performed by default, but HtmlEncode
in .NET versions before 4.0 did not encode the '
character. See HtmlEncode and UrlEncode Now Encode Single Quotation Marks.
Update: I did some googling and found out this interesting reference (Which ASP.NET Controls Automatically Encodes?) that may be outdated but at least for this specific case the documented behavior is the one you got.
As mentioned the ToolTip property of a ASP.NET control will auto encode the value on output/rendering.
This means it is safe to set the tooltip to plain text as the page will sanitize the text on rendering.
Label1.ToolTip = "Some encoded text < Tag >"
Renders HTML output as:
<span title="Some encoded text < Tag >"></span>
If you need to use text that is already encoded, you can set the title attribute instead:
Label1.Attributes("title") = "Some encoded text < Tag >"
Renders HTML output as:
<span title="Some encoded text < Tag >"></span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With