Solved

Should be very simple show/hide gridview element

Posted on 2007-11-15
5
565 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 74
IIS Express Working in one Visual Studio Solution, but not in another. Why? 2 21
Delete HTML table rows 12 29
IIS redirect 1 68
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 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