views:

66

answers:

3

In my .aspx.cs I have a code that reads a .xml file and I deserialize the xml into an object called Post. Problem is that in my .aspx page I have a div and I want to fill in the content of this div from code behind using the html generated from the code behind.. How can I do this?

+2  A: 

Use the InnerHtml or InnerText properties of the div to load your text. The div will be a control of type HtmlGenericControl.

In your page:

<div id="content" runat="server" />

In your codebehind:

protected HtmlGenericControl content;

content.InnerHtml = myGeneratedText;
Ray
so the name of the id of the div should match the variable name for the HtmlGenericControl?
EquinoX
Yes - and it must be protected (or public). If you have visual studio set up to automatically build a partial class, this will get done for you (I don't remember what this is called [web application project??] - I don't use it). Also, as Sky points out, there are other options - a Literal control is a good one. Or you can use code directly in the aspx file: <%= myGeneratedText %>.
Ray
This is good. But you will probably want to HtmlEncode myGeneratedText since it is html:content.InnerHtml = HttpUtility.HtmlEncode(myGeneratedText);
BritishDeveloper
+1  A: 

Use a Literal control. Example here.

Sky Sanders
+3  A: 

The div you mentioned from the aspx markup:

<div><asp:Literal ID="PostContent" runat="server" /></div>

Your code behind:

PostContent.Text = Post.GeneratedHtml;
Joel Coehoorn