Display HTML in ASP.NET Literal Control - asp.net

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?

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.

Related

Unable to edit text value in Sitecore

When hovered over the text 'Bedroom', the blue dotted box appears over it, but clicking on it, does not place the cursor, as if it is read-only.
However, I am able to edit the rendered images. And also the text 'Water View Loft' coming from another repeater, without issues.
ascx:
<asp:Repeater ID="rpPhotos" runat="server" ItemType="Sitecore.Data.Items.Item">
<ItemTemplate>
<div class="item">
<p>
<sc:Text runat="server" Field="Title" Item="<%#Container.DataItem %>"/>
</p>
<sc:Image runat="server" Field="Image" Item="<%#Container.DataItem %>" />
</div>
</ItemTemplate>
</asp:Repeater>
The fields Title & Image are from a template called Base Content. I have used this template to render other repeater in the same user control & there I can edit the text & images.
Why is this happening?
Try to remove the <p> tag, and see if the field gets editable. The Page Editor gets broken on nested <p> tags, and perhaps it happens also in your case.
Try to set the DisableWebEditing="false".

Databinding block generating only ampersands

I am trying to generate a number of characters (in this case stars) in a databinding block. All it is generating on the client side is ampersands (the correct number though).
<span class="review-stars">
<%# New String("★", Eval("CustomerReviewScore"))%>
</span>
I am not sure what I need to do for the characters to actually come up on the client.
EDIT:
HTML is:
<span class="review-stars">&&&&&</span>
You could use a Literal control instead and set it's Mode property to PassThrough:
<asp:Literal ID="Literal1"
Mode="PassThrough"
Text= '<%# New String("★", Eval("CustomerReviewScore"))%>'
runat="server">
</asp:Literal>

Display html text as html

There's a moment in my page where I connect to a database and retrieve some data. One field in special is html code. I want to display it in my page as html inside some control that understands it.
What are my options?
Use a literal.
.aspx:
<asp:Literal runat="server" ID="myLiteral" />
codebehind:
myLiteral.Text = "<h2> this is a h2 html tag</h2>";
this will print out
This is a h2 html tag

Asp .Net 4 and Literal output

Hello I found next strange as for me behavior of Literal in Asp MVC out put and I don't know how to fix it.
I have next part of html code on aspx page
...
<p class="description">
<asp:Literal ID="lblDescription" runat="server" EnableViewState="false"></asp:Literal>
</p>
...
On page load event I try to show user text:
lblDescription.Text = stringVar;
string var can contain other html tag like p, div, etc
But when my page loaded I see all html that should be inside was created under this element.
some text
Does any one know why ?
thanks
Try this:
<asp:Literal ID="lblDescription" Mode="PassThrough" runat="server" EnableViewState="false"></asp:Literal>
MSDN: Mode Property

'ImageButton' must be placed inside a form tag with runat=server

I've got the ImageButton wrapped inside a form tag with runat="server" but STILL getting this error at runtime. The form tag is at the very beginning (before my table) and end tag is at the end (after the table).
<td>
<div>
<div id="pay-Button"><asp:ImageButton ID="PayButton" ImageUrl="<%=PayButtonImageUrl %>" OnClick="RedirectTest" runat="server" /></div>
</div>
</td>
You can't have managed controls within a <form> tag without the runat='server'. ASP.Net supports multiple form tags, but with only one of them having a server-side designation. Also, I don't believe that HTML supports nested forms. You'll either want to have it be a non-managed control in a separate form or remove the nested <form> tag from the server-side form.
Look here for further explanation.
remove the ImageUrl="<%=PayButtonImageUrl %>" part, I don't believe you can use those inline snippits for server control properties (unless you're databinding)
Does your tag also have
runat="server"
as an attribute?
It would be helpful if you showed more of your code so we can help.

Resources