Hide/Show HyperLink in DataGrid

Posted on 2006-03-25
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

    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

    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

    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

    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

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now