Can't apply CSS class to DataGrid's HyperlinkColumn?

G

Guest

I have a hyperlink column in a datagrid as follows

<asp:HyperLinkColumn DataNavigateUrlField="OrderId" DataNavigateUrlFormatString="OrderDetails.aspx?Id={0}" DataTextField="Order" HeaderText="Order" ItemStyle-CssClass="Green10pt"></asp:HyperLinkColumn

renders
...
<td class="Green10pt"><a href=OrderDetails.aspx?Id=398365">ABC Corp</a></td>..

How can I get the class to be added to the anchor <a> tag not in the outer <td> element? ItemStyle-CssClass is not doing what I thought it would

Dave
 
M

Martin Dechev

Hi, Dave,

The easier way is to use a template column.

A little bit harder is to attach to the ItemCreated event of the DataGrid
and then cast appropriately the instance in e.Item.Cells[n].Controls[0] (in
VB e.Item.Cells(n).Controls(0) ) where n is the index (zero-based) of the
column. You should also check if e.Item.ItemIndex is different from -1 (-1
means it is the header or the footer row). Something like:

[C#]
protected void Item_Created(object s, DataGridItemEventArgs e)
{
if(e.Item.ItemIndex != -1)
{
// try HtmlControl or WebControl
((HtmlControl)e.Item.Cells[n].Controls[0]).Attributes["class"] =
"Green10pt";
}
}

[VB.NET]
Sub Item_Created(ByVal s As Object, ByVal e As DataGridItemEventArgs)
If e.Item.ItemIndex <> -1 Then
' try HtmlControl or WebControl
CType(e.Item.Cells(n).Controls(0), HtmlControl).Attributes("class") =
"Green10pt"
End If
End Sub

Hope this helps
Martin
Dave said:
I have a hyperlink column in a datagrid as follows:

<asp:HyperLinkColumn DataNavigateUrlField="OrderId"
DataNavigateUrlFormatString="OrderDetails.aspx?Id={0}" DataTextField="Order"
HeaderText="Order" ItemStyle-CssClass="Green10pt"> said:
renders:
...
<td class="Green10pt"><a href=OrderDetails.aspx?Id=398365">ABC
 

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

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top