Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

2 Answers 2

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
add comment

I believe it depends highly in what context it gets rendered. Try however the use method of the SPEncore, such as HtlmEncode http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.spencode.htmlencode

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? –  C. Marius - MVP Jul 29 '12 at 18:56
add comment

Your Answer

 
discard

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.