Solved

Should be very simple show/hide gridview element

Posted on 2007-11-15
5
564 Views
Last Modified: 2010-04-21
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
Comment
Question by:radleydp
  • 2
  • 2
5 Comments
 
LVL 37

Expert Comment

by:samtran0331
ID: 20293143
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
 
LVL 37

Accepted Solution

by:
samtran0331 earned 100 total points
ID: 20293159
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
 
LVL 16

Assisted Solution

by:GreymanMSC
GreymanMSC earned 100 total points
ID: 20294735
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
 

Author Comment

by:radleydp
ID: 20298076
Thanks guys, I had to make a slight tweak adding Handles GridView1.RowDataBound to the first line, but otherwise both solutions work great!
0
 

Author Closing Comment

by:radleydp
ID: 31409457
Perfect!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now