views:

1141

answers:

4

I have a DataGrid in ASP.NET 2.0 with the following column in it:

<ASP:TEMPLATECOLUMN>
    <ItemStyle HorizontalAlign="Right"></ItemStyle>   
    <ItemTemplate>
        <asp:HyperLink id="HyperLink1" runat="server" 
            CssClass="DataGridCommand" Target="_blank"
            NavigateUrl='<%# GetPreviewURL(DataBinder.Eval(Container, "DataItem.NodeID")) %>'>
        Preview Graphic
        </asp:HyperLink>
    </ItemTemplate>
</ASP:TEMPLATECOLUMN>

So the basic idea is to have a link in each row of the DataGrid that kicks back a pop-up image preview (referring to a data-bound column for the NodeID). This works just fine.

I am attempting to disable the links for the rows for which there is no associated image. To do this I put this in the code behind, under Page_Load:

foreach (DataGridItem dgi in this.dgNode.Items)
{
    HyperLink myLink1 = (HyperLink)dgi.Cells[0].FindControl("HyperLink1");

    //myLink1.Visible = false;
    //if (condition for hiding links goes here...)
    myLink1.Enabled = false;
    this.dgNode.DataBind();
}

As you can see, I tried setting the visible property to false, but that didn't work. I tried setting enable to false, it didn't work either. And then I tried databinding the DataGrid again -- doesn't work. The links continue to function normally.

Does anyone know why this doesn't work, what can be done to make it work, and if it's fundamentally flawed, an alternate solution? I'm looking to either grey out the link for certain rows, or hide it so that it can't be clicked.

Thanks!

A: 

I am not sure, but I think asp.net is rushing because you do have more than one instance of the object Hyperlink1. Isn't The ID proprety is used as unique identifier, if there is more than one instance maybe it refuses to manipulate it.

Erick
+1  A: 

Instead of looping thru the items on page load, you should add an event handler to handle the OnItemDataBound event. You can then find and disable the HyperLink there as each item is bound.

I believe the current solution is not working because it calls the DataBind() method after setting the link to invisible or disabled. This will cause the DataGrid to rebuild itself and is erasing the work that you just did. Not to mention you rebind your DataGrid for each item, which is a performance hit.

sgriffinusa
+1  A: 

I would wager that since you're disabling the hyperlinks and then binding the datagrid after the fact, that the rebinding is essentially "reactivating" the links again through its databind code.

As an alternative, have you thought about consuming the ItemDatabound event of the DataGrid and enabling/disabling the hyperlinks at that time? You'll have all the data right at your fingertips and the code will look pretty much the same as you have now.

Dillie-O
+1  A: 

You can handle this on the OnRowDataBound event, like so:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    HyperLink hl = (HyperLink)e.Row.FindControl("HyperLink1");
    hl.Visible = false;
}
Chris Pebble