Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView RowDataBound doesn't fire on postback

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?

like image 793
Ryan Lundy Avatar asked Nov 10 '08 22:11

Ryan Lundy


1 Answers

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.

like image 154
Rob Avatar answered Sep 29 '22 00:09

Rob