Textbox vs Label

M

MikeY

I found that both textbox and label renders diferently for the same string
that I have. The string looks like this:

"<something>"

Textbox renders is as it is but label sees it as a malform html tag.

I have use HTML.Decode method. Below is a sample code:

sample code:

string message1 = " <Something>";

StringWriter Message1 = new StringWriter();

Server.HtmlDecode( message1, Message1 );

this.TextBox1.Text = Message1.ToString();
this.Label1.Text = Message1.ToString();
this.txtarea.Value = Message1.ToString();

Label will not display the word <something> but if you view source you can
see it. Browser is assuming that it is a malform html tag.

I am using Label becuase this is a read only field and the data could
contain word like <something>

Any help is appreciated.
 
S

Scott M.

Yes, you need to encode the "<" and the ">" chars, so that they get sent to
the browser as "&lt;" and "&gt;". Do this as follows:

label.text = Server.HtmlEncode("<Something>")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top