0

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>";

}

2 Answers 2

1

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

2
  • I want to know why in ASP.NET by default the Text property value always be rendered as encoded but not in SharePoint Jul 29, 2012 at 16:06
  • There is no difference, it's the same control. ASP.NET isn't encoding either. Jul 29, 2012 at 16:38
0

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

2
  • I have updated my question, please check Jul 29, 2012 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? Jul 29, 2012 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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