Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Hide/Show HyperLink in DataGrid

Posted on 2006-03-25
5
Medium Priority
?
481 Views
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.

<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
0
Comment
Question by:Cragly
5 Comments
 
LVL 10

Expert Comment

by:aki4u
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?
0
 
LVL 22

Accepted Solution

by:
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
0
 
LVL 10

Assisted Solution

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

Author Comment

by:Cragly
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??

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

Many thanks

Cragly
0
 
LVL 10

Expert Comment

by:aki4u
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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