CuriousDev
CuriousDev

Reputation: 1275

Display HTML in ASP.NET Literal Control

I have the following ASP.NET Literal Control displayed on my page

<div class="col-md-8 text-justify">
    <p>
        <asp:Literal ID="Literal1"
            Mode="PassThrough"
            Text= "<%#:Item.Description %>"          
            runat="server">
        </asp:Literal>

    </p> 
</div> 

This control is bound to a field in my SQL Server Table and a column called Description in the table contains the text with HTML tags like <p> <ul> <br> etc.

However when the text is displayed in the Literal control, it is displayed as-is i.e the HTML does not get rendered.

If the Literal control does not support rendering, what other options do I have?

Upvotes: 3

Views: 7606

Answers (1)

mason
mason

Reputation: 32693

Your problem isn't with the Literal control. It's the way you embedded the HTML. Try this:

<asp:Literal ID="Literal1"
        Mode="PassThrough"
        Text= "<%# Item.Description %>"          
        runat="server">
    </asp:Literal>

Notice I left out the colon? That colon is there to prevent injection attacks, where the contents of the value being embedded might come from a user. But if you trust the HTML content, then removing the colon will embed the HTML and allow it to render properly instead of escaping it.

Upvotes: 6

Related Questions