• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

Should be very simple show/hide gridview element

I'm bashing my head using VB ASP.Net. This stuff is simple in ColdFusioon but I can't find the way to do this anywhere.

I have a gridview called GridView1. In this grid I have a TemplateField:
<asp:TemplateField>
                                        <ItemTemplate>
                                              <asp:LinkButton ID="LinkButton1" Visible="False" CommandArgument='<%# Eval("eadocsID") %>'
                                              CommandName="DeleteRow" runat="server"
                                              OnClientClick='return confirm("Are you sure want to delete this record?")'
                                              Font-Strikeout="False" Font-Bold="True" ForeColor="Green">Delete</asp:LinkButton>
                                        </ItemTemplate>
                                    </asp:TemplateField>

I have set visible to false. On the code behind, I'm trying to say if your cookie contains one of these values, set the Visible property to true:

If Request.Cookies("employee")("employeeid") = "241" Or "146" Or "8" Or "172" Then
            GridView1.Visible = True
        End If

Obviously, Gridview1.Visible isn't going to work, but I don't know how to reference LinkButton1 as it says it's not defined.

Please help!!!
0
radleydp
Asked:
radleydp
  • 2
  • 2
2 Solutions
 
samtran0331Commented:
you have to use the gridview's rowdatabound event, find the linkbutton using "findcontrol", then set it:

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lb As LinkButton
            lb = e.Row.FindControl("LinkButton1")
            If Request.Cookies("employee")("employeeid") = "241" Or "146" Or "8" Or "172" Then
                lb.Visible = False
            End If
        End If
    End Sub

Open in new window

0
 
samtran0331Commented:
actually, just realized that you can't use "Or" like that, so try:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim lb As LinkButton
            lb = e.Row.FindControl("LinkButton1")
            If Request.Cookies("employee")("employeeid") = "241" Or Request.Cookies("employee")("employeeid") = "146" Or Request.Cookies("employee")("employeeid") = "8" Or Request.Cookies("employee")("employeeid") = "172" Then
                lb.Visible = False
            End If
        End If
    End Sub

Open in new window

0
 
GreymanMSCCommented:
Just a slight tweak to the above solution.  It's far more elegant coding to use the Select condition to compare one item to a list of values.  It is much neater.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim lb As LinkButton = CType(e.Row.FindControl("LinkButton1"), LinkButton)
        If Not lb Is Nothing Then
            Select Case Request.Cookies("employee")("employeeid") 
            Case "241", "146", "8", "172" 
                lb.Visible = False
            Case Else
                lb.Visible = True 'just in case
            End Select
        End If
    End If
End Sub

Open in new window

0
 
radleydpAuthor Commented:
Thanks guys, I had to make a slight tweak adding Handles GridView1.RowDataBound to the first line, but otherwise both solutions work great!
0
 
radleydpAuthor Commented:
Perfect!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now