GridView RowCreated event not fired on selection; a.k.a. styles assigned in RowCreated handler lost

Discussion in 'ASP.NET 2.0' started by ASPmatters, May 28, 2007.

  1. I have a GridView in which I want to format rows two ways - 'Active' items are black
    text on white background, 'Archived' items are black text on gray background. I create
    this effect by assigning 'archived' rows to a cssclass 'archived' in the GridView's
    RowCreated event handler, and it works fine.

    Now, I'd also like to highlight the currently-selected row in this gridview with bright
    yellow. I can achieve this effect by specifying <SelectedRowStyle BackColor='Yellow' />
    in the .aspx file.

    Each of these effects works on their own, but not together. When I first render the
    gridview (nothing selected), the archived rows are correctly rendered with gray
    backgrounds, but when I click on <asp:ButtonField CommandName='Select'... />
    item in the gridview to select that row, I get the bright yellow highlighting on the
    selected row but I lose the gray background on the archived rows.

    I was surprised to learn that selecting a row causes a postback that triggers the
    SelectedIndexChanging and SelectedIndexChanged events *BUT NOT* the
    RowCreated event!?

    How can I have both attribute-sensitive row formatting and special selected-row
    formatting?

    Thanks,

    Chris
     
  2. I think it's due to the fact that I'm trying to format based on a value
    that I'm not actually displaying in the gridview. I include the 'archived'
    field in the query for setting up the databinding for the gridview, and
    it seems to still be 'available' when the rowcreated event is called
    (triggered by DataBind()), but on subsequent post-backs I don't
    re-bind the gridview so there's no rowcreated events and hence
    no styling of the rows based on the (now non-existent) archive
    field. It's still a bit of a mystery to me how the cssclass is 'lost'
    from the rows of the control on postback...

    I created a cumbersome workaround - would love to have ideas
    on more elegant ways to handle this: I added the 'archive' field
    as a gridview column; then on the page's prerender event I iterate
    over the rows of the gridview and style them based on the archive
    field value (True/False); then I hide the archive column just before
    leaving prerender, since I never actually want to see it; it *DOES*
    need to be 'visible', however, for this prerender processing to work,
    so I set it back to visible at the very beginning of the PageLoad
    handler. It's never actually visible to the user, but on each postback
    cycle it's 'visible' long enough to see its values and style each row
    in the prerender event.
     
  3. Trysetting the row styling in the RowDataBound event.

    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     

Share This Page