views:

418

answers:

3

Hi,

I have a gridview with some data and two hyperlinkfields I want to make the first hyperlinkfield of the first row not visible and the second hyperlinkfield of the last row not visible

this what I did till now

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim x As HyperLink
        x = e.Row.Cells(8).Controls(0)

        If e.Row.RowIndex = 0 Then
            x.Visible = False
        End If

        If e.Row.RowIndex = GridView1.Rows.Count Then
            'x = e.Row.Cells(9).Controls(0)
            'x.Visible = False
        End If
    End If
End Sub

This will work only for the first hyperlinkfield. Omitting the comments will make the first hyperlinkfield not visible for all rows.

Any help is appreciated. Thanks in advance.

A: 

By Looking at the code above , as you mentioned that when you uncomment the code which is commented,it will make the first hyperlinkfield not visible for all rows, as the gridview.Rows.Count is changing as it is firing for every row. You should try to do it where you Call the GridView1.DataBind(), mean after that statement you should check for the total rows and then find the hyperlink and disabled it. otherwise it will hide all the hyperlinks as its firing for every row and the GridView1.Rows.Count is changing.

OR

If you want to do that in the RowDataBound Event of the grid view then you can check for the DataControlRowType.Footer, DataControlRowType.Pager row and then get the last row by substracting 1 or 2, regarding your need and get the last row.

Hope that will help.

Asim Sajjad
A: 

you need to findcontrol from a particular row, like

protected void grd_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
        ((Hyperlink)e.Row.FindControl("HyperlinkID")).Visible = false;
    } 
} 
Muhammad Akhtar
+1  A: 

If the visibility is based on a data value you can use the Visible attribute in the HyperLink object.

           <asp:TemplateField HeaderText="Header" SortExpression="Field">
            <ItemTemplate>
              <asp:HyperLink ID="HyperLink1" runat="server" navigateUrl='http://www.google.com' Text='Google'
                    Visible='<%# Eval("flagShowLink") = "Yes"%>'></asp:HyperLink>
            </ItemTemplate>
           </asp:TemplateField>

You can also use an if statement inside the aspx file to display the HyperLink.

<%If Session("Access") > 6 Then%>
  <asp:HyperLink ID="HyperLink1" runat="server" navigateUrl='http://www.google.com' Text='Google'></asp:HyperLink>
<%End If%>
Jason Butler
+1 - This is the way to do it. Don't bother going into the code behind, just have it autobind to your criteria. If more complex criteria is necessary, then run it through public method in the code behind.
Joel Etherton