Label Control Doesn't HTML-Encode?


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">
<asp:HyperLink ID="lnkActivity" runat="server" >
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ActTitle")

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

"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

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?


Nov 11, 2012
Reaction score
<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