Solved

Should be very simple show/hide gridview element

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

13 Experts available now in Live!

Get 1:1 Help Now