Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 552841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:32:33+00:00 2026-05-13T11:32:33+00:00

I’m currently using a GridView and I want to set the CssClass for the

  • 0

I’m currently using a GridView and I want to set the CssClass for the Row depending on a property of the object that the row is being bound to.

I tried the following but it does not work (see comments):

<asp:GridView id="searchResultsGrid" runat="server" AllowPaging="true" PageSize="20" AutoGenerateColumns="false">

<!-- The following line doesn't work because apparently "Code blocks 
aren't allowed in this context: -->
  <RowStyle CssClass="<%#IIF(DataBinder.Eval(Container.DataItem,"NeedsAttention","red","") %>

  <Columns>
<!--............-->
  </Columns>
</asp:GridView>

Now I could simply handle the GridView’s RowDataBound event and change the css class of the row there…but I’m trying to keep a clear separation between the UI and the page/business logic layers.

I have no idea how to accomplish this and I’m looking forward to hearing any suggestions.

Thanks,

-Frinny

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:32:33+00:00Added an answer on May 13, 2026 at 11:32 am

    You cannot do this in declarative markup.

    Nearly all of GridView‘s declarative properties (including GridView.RowStyle) are grid-level settings rather than row-level. Apart from TemplateFields , they are not bound data containers, so they don’t have access to the data in their rows.

    If you want to keep this logic in the .aspx template, your only real option is to use template fields and manipulate their contents:

    <asp:TemplateField>
        <ItemTemplate>
            <span class="<%# ((string)Eval("property3")) == "NeedsAttention" ? "red" : string.Empty %>">
                <%# Eval("property1") %>
            </span>
        </ItemTemplate>                        
    </asp:TemplateField>
    

    Depending on what you want to do, this may be awkward – you don’t have access to the containing <td> (or <tr> for that matter) and you’ll have to repeat the formatting for each cell.

    The GridView class goes to a lot of lengths to hide the details of HTML and styling from you. After all you could create a GridView control adapter that wouldn’t even render as HTML tables. (Unlikely though that may be.)

    So even though you’re trying to avoid it, you’re probably best off dealing with this in a OnRowDataBound handler – or use a Repeater (if that’s appropriate).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.