Jake Petroules
Jake Petroules

Reputation: 24160

How do I encode HTML in an ASP.NET repeater?

I have an ASP.NET repeater pulling comment data from a database.

In my ItemTemplate I placed some Label server controls bound to the fields (username of poster, date, and post text), but apparently Label does not run the data through HtmlEncode before displaying it.

Is there another control I should use? How should I display HTML-encoded data from a repeater?

Upvotes: 2

Views: 2966

Answers (4)

Steven Webster
Steven Webster

Reputation: 21

This has worked for me:

<%# Server.HtmlDecode(DataBinder.Eval(Container.DataItem, "ItemName")) %>

Upvotes: 2

PMC
PMC

Reputation: 4766

What about literal with mode="encode"

<asp:Literal ID="Literal1" runat="server" Mode="Encode" />

Upvotes: 4

josephj1989
josephj1989

Reputation: 9709

You can use the literal control which has a mode property with enumeration Encode,PassThrough,Transform.

Upvotes: 2

scartag
scartag

Reputation: 17680

I'm assuming you want to be able to display the comments in html (with formatting et al).

replace the Label control with an Literal control. They both have the Text property but the control will handle your html content.

<asp:Literal>

Upvotes: -1

Related Questions