views:

5574

answers:

7

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?

A: 

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?'.

Si Keep
+1  A: 

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
A: 

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.

-Jason

Jason Hernandez
A: 

Do you have the grid inside an ajax UpdatePanel? That might be catching the postback.

Ricardo Villamil
Nope, no UpdatePanels on this particular page.
Kyralessa
A: 

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
A: 

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.

Kyralessa
A: 

`void Process Rows() {

... do something

... process complete

datagrid.DataBind();

}`