Adam Lynch
Adam Lynch

Reputation: 3369

how do I put an ID on the first table header in asp:DataGrid for CSS?

I want to do something in CSS specific to the first element in the table headers row but I don't want to have tot rely on :first_child.

Code:

<asp:TemplateColumn HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"> 
    <HeaderTemplate> 
        <span><asp:Literal id="header" runat="server" Text="<%$ Resources:index, IndexSearch_Header_Product %>" /></span> 
    </HeaderTemplate> 
    <ItemTemplate> 
        <asp:HyperLink runat="server" ID="productLink" /> 
    </ItemTemplate> 
</asp:TemplateColumn> 

Where can I add an ID (for CSS) on the td/th itself?

Upvotes: 0

Views: 514

Answers (3)

Lisetsky Val
Lisetsky Val

Reputation: 41

Use GridView instead of DataGrid. GridView renders header as 'th' tag (not 'td').

So, assuming you set CssClass="myGrid" to GridView you can decorate your header by this CSS selector:

table.myGrid tr th
{
   background-color: blue;
}

Upvotes: 0

suryakiran
suryakiran

Reputation: 1996

You can add the required class in the TemplateColumn as HeaderStyle-CssClass attribute. Check the below code.

<asp:TemplateColumn HeaderStyle-CssClass="firstheaderclass" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"> 
    <HeaderTemplate> 
        <span><asp:Literal id="header" runat="server" Text="<%$ Resources:index, IndexSearch_Header_Product %>" /></span> 
    </HeaderTemplate> 
    <ItemTemplate> 
        <asp:HyperLink runat="server" ID="productLink" /> 
    </ItemTemplate> 
</asp:TemplateColumn> 

But if you want to add an ID attribute to the first header of Datagrid, you have to do it in code behind and adding the ID attribute to the First header cell

Just give it a try ;-)

Upvotes: 1

Kinexus
Kinexus

Reputation: 12904

Not sure you can. Create a named css class and assign this to your content within the HeaderTemplate.

Upvotes: 0

Related Questions