Search code examples
c#asp.nethtmlabbr

Strange tag being generated in C#


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.


Solution

  • 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 :)