Microsoft DN
Microsoft DN

Reputation: 10030

How to display HTML content in C# string?

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?

Upvotes: 4

Views: 12611

Answers (3)

norbertVC
norbertVC

Reputation: 475

Use @Html.Raw()

@Html.Raw(string);

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

Upvotes: 1

Fedor Hajdu
Fedor Hajdu

Reputation: 4695

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.

Upvotes: 0

kelsier
kelsier

Reputation: 4100

Try HtmlString like:

HtmlString html = new HtmlString("Hello <b> World ! </b>");

Upvotes: 1

Related Questions