How do I add a CSS class to a BoundField, so I can find it with jQuery?

pencilCake picture pencilCake · Aug 12, 2010 · Viewed 48k times · Source

I want to add a class name to some of my BoundFields in the GridView control; so that once the GridView is data-bound and rendered I can obtain something like:

<td class="Tag1">Some data came from data source</td>

The purpose of doing such a thing is to be able to find all the elements that are "Tag1" in this way:

var allTag1td = $('td.Tag1');

So, how can I add this class to the BoundField so that it is rendered in this way?

Answer

Ralph Willgoss picture Ralph Willgoss · Dec 16, 2011

Add the ItemStyle property to your field:

<asp:BoundField DataField="Count" HeaderText="Count">
    <ItemStyle CssClass="yourclass"></ItemStyle>
</asp:BoundField>