Learn how to a build a cloud-first strategyRegister Now


Hide/Show HyperLink in DataGrid

Posted on 2006-03-25
Medium Priority
Last Modified: 2008-02-01
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

Question by:Cragly
LVL 10

Expert Comment

ID: 16290169
how do you determine if to show/hide link? Is there a value in datatable? If so, what's the field name and type?
LVL 22

Accepted Solution

Jason Scolaro earned 1000 total points
ID: 16291117
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
LVL 10

Assisted Solution

SystemExpert earned 1000 total points
ID: 16292126
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;


Author Comment

ID: 16293377
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

LVL 10

Expert Comment

ID: 16293911
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.

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question