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

Discussion in 'ASP .Net' started by =?Utf-8?B?RGF2ZQ==?=, Feb 27, 2004.

  1. 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
    =?Utf-8?B?RGF2ZQ==?=, Feb 27, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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, Feb 27, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bnN2a2V5YW4=?=

    how to apply css class style to web controls

    =?Utf-8?B?bnN2a2V5YW4=?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,667
    Chris Jackson
    Feb 20, 2004
  2. Maziar Aflatoun

    Apply css to asp:HyperLinkColumn

    Maziar Aflatoun, Jul 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,840
    Eliyahu Goldin
    Jul 14, 2004
  3. =?Utf-8?B?Q3J5c3RhbA==?=

    How can I apply this CSS to the asp.net?

    =?Utf-8?B?Q3J5c3RhbA==?=, Sep 23, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,190
    =?Utf-8?B?Q3J5c3RhbA==?=
    Sep 24, 2004
  4. Stefan Siegl
    Replies:
    1
    Views:
    951
    Marrow
    Jul 18, 2003
  5. Waffeloo

    Apply style changes to a whole CSS class

    Waffeloo, Sep 19, 2005, in forum: Javascript
    Replies:
    3
    Views:
    108
    Waffeloo
    Sep 20, 2005
Loading...

Share This Page