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
CraglyAsked:
Who is Participating?
 
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
0
 
aki4uCommented:
how do you determine if to show/hide link? Is there a value in datatable? If so, what's the field name and type?
0
 
SystemExpertCommented:
Hi,
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;

Thanks
0
 
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??

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21787495.html

Many thanks

Cragly
0
 
aki4uCommented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.