Question

I have the following GetText() function, which relates to my question, which is being called in the following place:

myGridView.DataSource = stuff.Select(s => new
{
   //...some stuff here
   f.Text = GetText();
}
myGridView.DataBind();

GetText looks like the following:

private void string GetText()
{
    StringBuilder sb = new StringBuilder();
sb.Append("<abbr title=\"Testing\">");
    sp.Append("This is the Text that I want to display");
sb.Append("<\abbr>");
}

So essentially, all I want to do is be able to have the following HTML on my webpage:

<abbr title="Testing">This is the text that I want to display</abbr>

However, there is a mysterious tag that shows up. In google chrome, I looked at th the console and I saw that it looked like this:

<abbr title="Testing">This is the text that I want to display<bbr></abbr>

There is an extraneous tag that is generated when I add in the line sb.Append("<\abbr>"); This is fixed when I remove that line, but I would like to find a better solution since this makes the code look awkward.

I also tried doing the following instead of the multi-lined sb.Appends() but the extra text is still shown.

sb.Append(string.Format("<abbr title=\"testing\">{0}<\abbr>",Text));

NOTE: Assume that Text is a string which equals the text that I want to display.

Was it helpful?

Solution 3

Here's how your method shoul like

    private string GetText()
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<abbr title=\"Testing\">");
        sb.Append("This is the Text that I want to display");
        sb.Append("</abbr>");

        return sb.ToString();
    }

"\" is an escape character :)

OTHER TIPS

Your end tag is wrong. It should be </abbr> not <\abbr>.

<\abbr> will include an escaped a (which means nothing), inside the <bbr>. Chrome apparently closes the <abbr> tag. So the superfluous tag is actually </abbr> not <bbr>.

Use

sb.Append("</abbr>");

probably the \abbr is interpreted as an escape char \a followed by the bbr> text

By the way, looking at the escape sequences on MSDN it seems that \a is the escape sequence for the BELL character. (No, I don't think that you should hear a beep from your PC)

Just a guess but is it because you have a backslash instead of slash in your closing tag?

sb.Append("<\abbr>");

vs

sb.Append("</abbr>");

As others have said, this is the issue:

sb.Append("<\abbr>");

... but it's worth looking at exactly what's happening.

That's appending <, then a U+0007 (the "alert" character, or bell), then bbr>. If you'd done this with a character which wasn't a valid escape character (e.g. "<\zfoo>") then you'd have received a compile-time error. In some other cases you might have been able to see it in the HTML. It's only because you picked a completely invisible control character that it was harder to see.

As an aside, I don't think I've ever seen a C# program which needed \a. I wish it wasn't a valid escape character - along with the \x hex sequence...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top