tags:

views:

279

answers:

2

In this question, I was given a really cool answer to alternating an image and its description between left and right, respectively. Now I want to apply styling to both, e.g. padding-top, padding-bottom etc. How do I apply a style to both the RowStyle and AlternatingRowStyle in this scenario.

<AlternatingRowStyle CssClass="ProductAltItemStyle" />   
<RowStyle CssClass="ProductItemStyle" />
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <div class="Image"><asp:Image runat="server" ID="productImage" ImageUrl='<%# Eval("imageUrl") %>' /></div>
            <div class="Description"><asp:Label runat="server" ID="lblProductDesc" Width="100%" Text='<%# Eval("productDesc") %>'></asp:Label></div>
        </ItemTemplate>
    </asp:TemplateField>
+16  A: 

Here's how you do it:

.ProductAltItemStyle, .ProductItemStyle {
    // CSS Rules that apply to both go here
}
changelog
Small addition, as commented on another CSS question, personally I think having a line-break between each selector (and before the "{" ) is a more obvious readable, searchable format. CSS block formatting is unlike C/C#/Java/JavaScript in this respect.
annakata
+2  A: 

Alternatively you can do this:

<AlternatingRowStyle CssClass="ProductAltItemStyle ProductCommonStyle" />   
<RowStyle CssClass="ProductItemStyle ProductCommonStyle" />

ProductCommonStyle contains formatting that is common to both alternating and standard rows.

Even better, you can assign a style to your whole gridview, and use that to define the shared classes:

table.GridViewStyle tr td
{
   padding:3px 5px;
   border:1px solid gray;
}

tr.ProductAltItemStyle td
{
    background:white;
}

tr.ProductItemSTyle td
{
    background:silver;
}
Herb Caudill
I think this is probably the actual answer to this specific question, not that changelog is wrong, but I'm a big believer in CSS following an OO model.
annakata