4

I am trying to populate a gridview from sharepoint list and population is working good expect in one column i want to display and <a> tag

I have the following code:

   StringBuilder cstext = new StringBuilder();

   dr["Status"] = cstext.Append("<a>click me</a>"); 

but in my browser i am seeing this <a>click me</a> and in inspect element i am seeing this &lt;a&gt;click me&lt;/a&gt;

Please help me

  • can you try it as cstext.Append("<a>click me</a>").toString(); ? – Gautam Sheth Jul 11 '17 at 8:33
  • @GautamSheth: ToString won't help, as it's called anyway before rendering. The problem probably is HTMLEncode-ing the content – Nils Jul 11 '17 at 8:36
  • 2
    A StringBuilder is for Building strings, and the Append() methods return value is intended for chaining (value.Append("a").Append("b") ). Using it in the way you are using it (like a regular System.String) is probably going to lead to other unwanted side effects. – StingyJack Jul 11 '17 at 12:15
6

I'm guessing your problem is not StringBuilder, but the "gridview". Are you using a SPGridView?

If so, the grid is probably encoding the content you put there. If you want to add HTML content in a cell set the HTMLEncode-property of your BoundField to false.

Also, on a sidenote, dr["Status"] = cstext.Append("<a>click me</a>"); will set the content of the cell to the StringBuilder. StringBuilder.Append will return the whole StringBuilder.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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