4

I am trying to achieve something like this:

string html = @"Hello <b> World ! </b>";

The desired output would be

Hello World !

The string html can be used anywhere on label, textbox, etc.

But it is not working. It just displays as it is Hello <b> World ! </b>

Is there any other way to do this?

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
Microsoft DN
  • 9,706
  • 10
  • 51
  • 71

3 Answers3

1

Try HtmlString like:

HtmlString html = new HtmlString("Hello <b> World ! </b>");
kelsier
  • 4,050
  • 5
  • 34
  • 49
  • I cannot find HtmlString in C# – Microsoft DN Mar 12 '14 at 07:37
  • Check http://msdn.microsoft.com/en-us/library/system.web.htmlstring(v=vs.110).aspx. – kelsier Mar 12 '14 at 07:41
  • 1
    And why the negative vote? I just tested it in an asp.net mvc 4 application and it works fine. – kelsier Mar 12 '14 at 07:41
  • I didn't downvote you, but I don't think this question is related to MVC. – Fedor Hajdu Mar 12 '14 at 08:13
  • @MicrosoftDN It works in web forms too, I just tested it. Can you show me some example code like how you are planning to use it? The following code works for me (lbl is a ``): `HtmlString html = new HtmlString("Hello World ! "); lbl.Text = html.ToString();` – kelsier Mar 12 '14 at 08:46
1

Use @Html.Raw()

@Html.Raw(string);

See here for more: http://forums.asp.net/t/1903975.aspx?how+to+use+html+raw

Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
norbertVC
  • 425
  • 1
  • 4
  • 12
0

Depends on the version of ASP.NET, but your safest bet is to create a literal control

<asp:Literal runat='server' id='yourOutput' Text='' />

And then set it on code behind

yourOutput.Text = html;

This should work on all versions of classic ASP.NET - for MVC projects you already got good answers.

Fedor Hajdu
  • 4,657
  • 3
  • 32
  • 50