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

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?

What about literal with mode="encode"
<asp:Literal ID="Literal1" runat="server" Mode="Encode" />

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

This has worked for me:
<%# Server.HtmlDecode(DataBinder.Eval(Container.DataItem, "ItemName")) %>

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>

Related

ASP.net insert html encoded code into page

I have a dim which contains html encoded code, which is stored in DB.
How can I insert the code into asp.net page?
I would like to perform the action in code behind ( VB.net / C# ).
What control/method should I use. Please note I want the code to be rendered by the browsers – not just displayed.
Assuming web forms, you can use a Literal control on your web page and set its Mode property to PassThrough.
PassThrough meaning
The contents of the control are not modified.
as opposed to the default Transform
The contents of the control are converted to an HTML-encoded string.
Example aspx:
<asp:Literal ID="Literal1" Mode="PassThrough" runat="server"></asp:Literal>
Code-behind:
Me.Literal1.Text = "<p>I'm a paragraph.</p>"

Eval inside an ASP.net repeater doesn't work inside another control

I have a Reapter control with a custom server side control inside of it. When I place the code:
<%# Eval("DateAdded") %>
inside the item template of the repeater it works fine, but when I place it inside the custom server control inside the repeater, it doesn't work.
Is it possible to do something like Parent.Eval() or Container.Eval() to get back to the context of the Reapeter rather than the custom control?
The repeater is databound, the control that you are placing this eval statement in isn't. You'll need to pass the value to the control using a property.
For Example:
<uc1:MyControl MyProperty='<%# Eval("DateAdded") %>' />
You can now access the MyProperty property inside your control to access this value.
It should work. Can you supply more context/code for the server control?
Also, are you using single quotes to set properties on a control dynamically using eval? Double quotes should throw an error, not just be ignored.
MyProperty='<%# Eval("DateAdded") %>'

Multiline textbox - "Code blocks are not supported in this context"

I have a multiline (> 50 lines) textbox containing plain text. I want to add either a session variable to the text in this box. In asp I would have done it by putting in <% %> code blocks but in .net I am getting the following error: "Code blocks are not supported in this context". I assume therefore that this would need doing in code behind.
Is there a quicker way than putting all the text from the textbox in a string in code-behind and then adding the variable on like this? I would like to keep the text in my aspx page if possible.
Thanks
How about your codebehind does something like:
myTextbox.Text += Session ["mySessionVariable"];
after you've filled the textbox.
Incidentally, you don't have to
'put all the text from the textbox in
a string in codebehind'
as the .Net framework exposes all the front-end controls as codebehind objects automatically.
EDIT:
<asp:TextBox ID="TextBox1" runat="server" Rows="15" TextMode="MultiLine" Columns="70" Text='<%# Session["var1"] %>'></asp:TextBox>
This will work for binding just the session variable to the control. Don't forget to call
Page.DataBind();
after you've set your Session variables. Probably in Page_Load.
This will allow the binding such as it is, to occur. This won't work if you want to mix up static markup text with dynamic variables. For that, you'll need to get busy in the code-behind.
HTH.
Have you tried <%=Session["MySessionKey"] %> ?

Asp.Net - User control with text between blocks

I want to create a usercontrol that behaves like the Label usercontrol or the HyperLink usercontrol.
What I mean - the Label usercontrol has the Text attribute, and the text can also be set with the following way:
<asp:Label runat="server" id="lblTest">Text Here</asp:Label>
If I wish to create a usercontrol that can set the text between blocks to the actual Text attribute of the control.
Do I need to use Templated UserControl? If no - what is the correct way?
Thanks all.
I would create a simple custom control with the text attribute exposed as a property :-)
http://msdn.microsoft.com/en-us/library/aa310915%28VS.71%29.aspx

CheckboxList - Use web.config value in text field

The scenario I'm dealing with is I have a set of entries in a database that have an image associated with them. This image needs to be 'accepted' or 'rejected' via a web interface.
I'm using ASP.NET WebForms.
I'm attempting to databind my recordset of entires to a CheckBoxList control. I would like to combine data from my dataset with information from the web.config file and plain text to display an image:
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="DataSource1"
DataTextField="ImageIdentifier"
DataValueField="EntryId"
DataTextFormatString="<img src='<%$ AppSettings:GetImageUrl %>{0}' />" />
This approach correctly outputs the plain text and the DataTextField value, however it does not interpret the code inside the <% %> block and prints the whole thing literally in the generated HTML, in spite of it being correctly highlighted in the editor.
Is this possible to achieve declaratively? If not, is there a better way than iterating through the entries in the list in code on the OnDataBound event?
Thanks in advance,
Jamie
I believe you're using the wrong <% tag. In order to evaluate in a binding expression like that it should be <%#
What does your web.config look like? You'll won't be able to use that binding syntax here - you'll have to hook into the databound event of the checkbox and iterate each checkbox, updating them as necessary from the values in your web.config.
EDIT
If you don't want to iterate each checkbox after the checkboxlist has been databound, you'll have to update your dataset first, before you bind to it.

Resources