SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a simple Webpart which has label control displaying some text, but I don't know why Label control's text is not rendered as html encoded. I have worked with ASP.NET site development and it encodes in it.

So my questions that why it is left not to encode in SharePoint but Asp.Net sites.

protected override void CreateChildControls()
    Label myLable = new Label();
    myLable = "<h1>Hello World</h1>";

share|improve this question

If you just want to insert the text encoded into the HTML then it's better to use a Literal control and set the Mode:

protected override void CreateChildControls() 
    var myLiteral = new Literal (); 
    myLiteral.Mode = LiteralMode.Encode;
    myLiteral.Text = "<h1>Hello World</h1>"; 


The Label controls purpose in ASP.NET isn't to render plain text, but to render text used as a Label for an Associated Control. If you want that text encoded you need to encode it yourselves using HttpServerUtility.HtmlEncode or SPEncode.HtmlEncode

share|improve this answer
I want to know why in ASP.NET by default the Text property value always be rendered as encoded but not in SharePoint – AmitKB Jul 29 '12 at 16:06
There is no difference, it's the same control. ASP.NET isn't encoding either. – Per Jakobsen Jul 29 '12 at 16:38

I believe it depends highly in what context it gets rendered. Try however the use method of the SPEncore, such as HtlmEncode

share|improve this answer
I have updated my question, please check – AmitKB Jul 29 '12 at 16:06
That is all that your web part is doing? Could you please drop more details,e.g. in what control are you dropping this Label? – Marius Constantinescu - MVP Jul 29 '12 at 18:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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