开发者

How to change the visibility of a hyperlinkfield in a gridview?

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)
    开发者_Python百科        '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.


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%>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜