Pergunta

I have a problem with InnerHTML property of a HtmlGenericControl object. I thought that if I assign "<b>xx</b>" to a InnerText property it will get encoded - and it does - and if I assign the same string to InnerHtml it will not get encoded - but it does!! I am confused as it goes against what I have seen here in other posts.

Background: The string that I am assigning is loaded from database and assigned to various other strings during its lifetime and then finally replaces a placeholder with Replace method. Still it contains "<b>xx</b>" rather than encoded version... To be absolutely sure I bypassed the entire process and assigned "<b>xx</b>" string directly in code behind - the result still remains the same (i.e. encoded).

Could someone please explain why this is happening? What is it that I am doing wrong? thanks.

EDIT to make the issue 100% clear: I need to programatically assign "<b>xx</b>" into HTMLGenericControl object and have it rendered as HTML tags and not as encoded text. What happens is I can see "<b>xx</b>" written on the screen - not desired, I want to see bold text. I thought (and it is described in documentation as such) that if I assign to InnerHtml property that the string is not encoded and displayed "as-is". It however does not behave so.

Foi útil?

Solução

If it is feasible in your situation to replace the HTMLGenericControl with a ASP:Literal control I would do that.

I just had a similar situation come up upgrading a CMS system and that was how I handled it.

Outras dicas

var sb = new StringBuilder();
editable.RenderControl(new HtmlTextWriter(new StringWriter(sb)));
stringDiv = sb.ToString();
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top