Link to home
Start Free TrialLog in
Avatar of Cragly
Cragly

asked on

Hide/Show HyperLink in DataGrid

Hi All,

I want to be able to hide or show a hyperlinks in a template column within a DataGrid. I select users from a database and bind them to the grid. If a user can not be edited or deleted I want to be able to hide the edit/delete hyperlinks for that particular record in the data grid. I have included my template column code below. If there is a better way of achieving the same result then I am happy to change my code.

<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
  <ItemTemplate>
    <asp:HyperLink id="hlkUpdate" runat="server" NavigateUrl='<%# "usersUpdate.aspx?uid=" + DataBinder.Eval(Container.DataItem, "UserID") %>'>edit</asp:HyperLink> |
    <asp:HyperLink id="hlkDelete" runat="server" NavigateUrl='<%# "usersDelete.aspx?uid=" + DataBinder.Eval(Container.DataItem, "UserID") %>'>delete</asp:HyperLink>
  </ItemTemplate>
</asp:TemplateColumn>

Many thanks in advance

Cragly
Avatar of aki4u
aki4u

how do you determine if to show/hide link? Is there a value in datatable? If so, what's the field name and type?
ASKER CERTIFIED SOLUTION
Avatar of Jason Scolaro
Jason Scolaro
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Cragly

ASKER

Thanks very much guys for your help it worked a treat. Here is the final code used for anybody with the same problem.

private void dgCategories_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
      {
            DataRowView row = (DataRowView)e.Item.DataItem;;

            if (row["CanEdit"].ToString() != "1")
            {
                  ((HyperLink)e.Item.Cells[0].FindControl("hlkUpdate")).Enabled = false;
                  ((HyperLink)e.Item.Cells[0].FindControl("hlkDelete")).Enabled = false;
            }
      }
}

You could not help me on another issue that I have posted could you??

https://www.experts-exchange.com/questions/21787495/C-ASP-NET-Checkbox-List-Problem.html

Many thanks

Cragly
Why even go to dgCategories_ItemDataBound when you can hide them in html view?!
That's why I asked you to provide me details in my first posr but you never answered.