Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Dave
Guest
 
Posts: n/a
#1: Nov 18 '05
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


Martin Dechev
Guest
 
Posts: n/a
#2: Nov 18 '05

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


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" <anonymous@discussions.microsoft.com> wrote in message
news:A8DD384D-C642-4B00-BC6F-FFDD8E1D8D23@microsoft.com...[color=blue]
> I have a hyperlink column in a datagrid as follows:
>
> <asp:HyperLinkColumn DataNavigateUrlField="OrderId"[/color]
DataNavigateUrlFormatString="OrderDetails.aspx?Id= {0}" DataTextField="Order"
HeaderText="Order" ItemStyle-CssClass="Green10pt"></asp:HyperLinkColumn>[color=blue]
>
> renders:
> ...
> <td class="Green10pt"><a href=OrderDetails.aspx?Id=398365">ABC[/color]
Corp</a></td>...[color=blue]
>
> How can I get the class to be added to the anchor <a> tag not in the outer[/color]
<td> element? ItemStyle-CssClass is not doing what I thought it would.[color=blue]
>
> Dave.
>[/color]


Closed Thread


Similar ASP.NET bytes