GridView RowDataBound doesn't fire on postback

18,042

Solution 1

Here's how I ended up solving this:

  1. I created a serializable class with readonly properties: PK of a row, and a boolean for each link indicating whether it's enabled or not. We'll call it LinkVisibility.
  2. I created a serializable class inheriting from KeyedCollection to hold instances of the class above.
  3. I created a ViewState-backed property holding an instance of that collection.
  4. In my Search procedure (populating the GridView), I clear the collection.
  5. In RowDataBound, which initially shows/hides the links, I add a LinkVisibility instance to the collection for each row.
  6. In Page.Load, when IsPostBack is true, I loop through the GridView rows. I look up the LinkVisibility for each one by PK in the collection (DataKeyNames is set in the GridView), and I set the links accordingly.

I don't know that this is the best way to do this, but it certainly does work, which is more than I can say for anything else I've tried.

Solution 2

The RowDataBound event only fires when the GridView's data changes during the postback. The event is short-circuited for speed so it's not re-generating the exact same data unnecessarily. Use the RowCreated event to manipulate the HTML instead - it fires on every postback regardless of whether the data has changed.

Solution 3

1) You could have a Method - ProcessDataRows() that would get called once on grid_DataBound(...). And then when you need it after PostBack.

And that way you process all rows when you want.

2) You could have methods like ShowParentLink(). That are then bound to the LinkButton in the grid (if you're using an ItemTemplate) and the link would have

Visible='<%#ShowParentLink()%>'
Share:
18,042
andriy
Author by

andriy

A .NET developer in the Germany/France/Switzerland border area. I do web development, desktop development, Selenium automation, and lots more. Currently employed at Dreamlines GmbH. I can help you transition your team to Git automate your regression tests release your next great ASP.NET MVC, WPF, or even WinForms app chase down those hard-to-catch bugs write tests for things that are hard to test I speak fluent English, excellent Romanian, and pretty good German. And a smattering of French, at least enough to tell the airport taxi drivers how to find my house.

Updated on August 05, 2022

Comments

  • andriy
    andriy over 1 year

    On an ASP.NET page, I have a GridView populated with the results of a LINQ query. I'm setting the DataSource in code, then calling DataBind on it. In the GridView's RowDataBound event, I'm selectively hiding links in some GridView fields based on the query results. (For instance, I hide the "Show Parent" link of the row in question has no parent row.)

    This works fine initially. But on postback (when I don't call DataBind, but the GridView stays populated through ViewState), the data displays, but the RowDataBound event (obviously) doesn't fire, and my links don't get hidden.

    What's the best way to get the links to be hidden after a postback?

  • ForceMagic
    ForceMagic over 11 years
    Welcome on SO, here, it is a good practice to explain why to use your solution and not just how. That will make your answer more valuable and help further reader to have a better understanding of how you do it. I also suggest that you have a look on our FAQ : stackoverflow.com/faq.
  • dlchambers
    dlchambers about 4 years
    But, sadly, e.Row.DataItem is null on RowCreated whereas it's non-null on RowDataBound :(