8

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?

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211

8 Answers8

3

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.

Rob
  • 31
  • 1
2

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.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
1

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()%>'
sirrocco
  • 7,975
  • 4
  • 59
  • 81
0

I would have expected the viewstate to also reflect the fact that you have removed some of the links (assuming that they were removed before viewstate was saved).

Maybe thats the question you need to ask 'why do the removed links still appear in viewstate?'.

Simon Keep
  • 9,886
  • 9
  • 63
  • 78
0

A page cannot process postback events unless it is rebuilt exactly as it was before (the postback). If you re-hide your links during the page-init, then your click events and such should fire. Unfortunately, without seeing some sample code I can't get more specific.

Also the data RowDataBound does not fire because you are not data binding. You are rebuilding the page from the viewstate- "viewstate binding" for lack of a better word.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
Jason Hernandez
  • 2,907
  • 1
  • 19
  • 30
0

Another solution is to put the logic in the LINQ query, so that you end up with a boolean LINQ field like "ShowParentLink". Then you can just bind the Visible property of the HyperLink field to that value - no RowDataBound required.

gfrizzle
  • 12,419
  • 19
  • 78
  • 104
0
void Process Rows()
{
    ... do something
    ... process complete
    datagrid.DataBind();
}
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
0
protected void btnHazardRating_Click(object sender, EventArgs e)
{
    gvPanelRole.RowDataBound += new GridViewRowEventHandler(gvPanelRole_RowDataBound);

    gvPanelRole.DataSource = dtGo;
    gvPanelRole.DataBind();
    ModalPopup.Show();

}
raveturned
  • 2,637
  • 24
  • 30
  • 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 : http://stackoverflow.com/faq. – ForceMagic Oct 26 '12 at 04:51