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?

8
  • 1
    @SonerGönül: I don't think it's related as that link is to handle Bold only but OP is asking something dynamically if I'm not wrong Commented Mar 12, 2014 at 7:20
  • 2
    Are you doing this in a WinForms application? WPF? ASP.Net? This is quite important to know if we are to help you :-) Commented Mar 12, 2014 at 7:21
  • Try this: stackoverflow.com/questions/2977675/… Commented Mar 12, 2014 at 7:22
  • What is not working ? - what error do you get, or you can not find the string ? Commented Mar 12, 2014 at 7:24
  • I dont want this string to be placed on only web form.. want to use this string on different places like on label, or on textbox, or inside a dynamic table etc Commented Mar 12, 2014 at 7:25

3 Answers 3

1

Try HtmlString like:

HtmlString html = new HtmlString("Hello <b> World ! </b>");
Sign up to request clarification or add additional context in comments.

5 Comments

I cannot find HtmlString in C#
And why the negative vote? I just tested it in an asp.net mvc 4 application and it works fine.
I didn't downvote you, but I don't think this question is related to MVC.
@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 <asp:Label ID="lbl" runat="server"></asp:Label>): HtmlString html = new HtmlString("Hello <b> World ! </b>"); lbl.Text = html.ToString();
1

Use @Html.Raw()

@Html.Raw(string);

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

2 Comments

The OP really want MVC?
Oh sorry, I generelly use MVC if possible.
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.

1 Comment

As I already said.. I want this string to put on different controls not onlyin literal

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.