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.

<ItemStyle HorizontalAlign="Center"></ItemStyle>
    <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>

Many thanks in advance

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

how do you determine if to show/hide link? Is there a value in datatable? If so, what's the field name and type?
Jason ScolaroCommented:
Basically all you need to do since you have the TemplateColumn already setup, is put in logic to determine which rows to hide the controls on and which rows to show them.

In your ItemDataBound event for the DataGrid, you'll have some code that looks like this:

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
  Dim row As DataRowView = CType(e.Item.DataItem, DataRowView)

  If row("MyField") <> "CanShow" Then
    e.Item.FindControl("hlkUpdate").Visible = False
    e.Item.FindControl("hlkDelete").Visible = False
  End If
End If

Couple things to point out... 1st - DataRowView, this may change depending on what data source you are using to bind to the DataGrid, so be aware.  2nd - row("MyField") <> "CanShow", this is basic logic that determines whether to show the 2 controls or not, you will need to tweak this to match your appropriate logic.

Good luck!
-- Jason

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
in your ItemBound Event you can use like this

SCOLJA is right but you have to Cast to (LinkButton)

((LinkButton) e.Item.Cells[0].FindControl("YourLinkButton")).Visible =false;

CraglyAuthor Commented:
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??


Many thanks

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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.