Label Control Doesn't HTML-Encode?


J

Jonathan Wood

I'm using a GridView control with a TemplateField column. It works fine
except I see the text is not HTML-encoded.

Here's the column template:

<asp:TemplateField HeaderText="Title" SortExpression="ActTitle">
<ItemTemplate>
<asp:HyperLink ID="lnkActivity" runat="server" >
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ActTitle")
%>'></asp:Label>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

When editing GridView columns, there is an option to specify whether or not
the data is encoded. But this is not the case with templated columns.

I've always known the Label control to HTML but, according to
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.label.text.aspx:

"The Text property can include HTML. If it does, the HTML will be passed
unchanged to the browser, where is might be interpreted as markup and not as
text."

I thought if I could move the Bind() to the inner text of the label control
that might resolve this but I get an error saying Bind can only be assigned
in a control property.

My datasource is a DataSet so the only way I could manually encode it is by
iterating through the entire dataset.

Does anyone know how I can get this data HTML-encoded?

Thanks.

Jonathan
 
Ad

Advertisements

Joined
Nov 11, 2012
Messages
1
Reaction score
0
<asp:Label runat="server" Text='<%# Server.HtmlEncode((string)Eval("ActTitle")) %>'></asp:Label>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top