如何在asp.net中启用和禁用gridview中的按钮

时间:2015-04-23 10:08:14

标签: asp.net vb.net gridview

我目前有一个带有两个按钮的GridView,我使用以下代码添加了这些按钮;

<asp:GridView ID="gvResults" AutoGenerateColumns="False" runat="server" Font-Size="small"
        DataKeyNames="BeachHutID" OnRowDataBound="gvResults_RowDataBound" CssClass="BBCSearch">
        <Columns>
            <asp:BoundField DataField="BeachHutID" SortExpression="BeachHutID" Visible="false">
            </asp:BoundField>
            <asp:ImageField DataImageUrlField="HutImage" ItemStyle-Width="1%" ReadOnly="true" />
            <asp:BoundField HeaderText="Facilities" DataField="Facilities" Visible="false"></asp:BoundField>
            <asp:BoundField HeaderText="Info" DataField="Info" Visible="false"></asp:BoundField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:PlaceHolder ID="lblHolder" runat="server"></asp:PlaceHolder>
                    <br />
                    <asp:PlaceHolder ID="imgHolder" runat="server"></asp:PlaceHolder>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button ID="btnHire" CommandArgument='<%# Eval("BeachHutID") %>' runat="server"
                        Text="Hire Beach Hut" OnClick="Hire_Click" />
                        <asp:Button ID="ButtonLogin" CommandArgument='<%# Eval("BeachHutID") %>' runat="server"
                        Text="Login to Hire Beach Hut" OnClick="Login_Redirect" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

我想要发生的是,如果用户已登录,则应启用btnHire按钮并显示,如果他们未登录,则显示ButtonLoggedIn并隐藏btnHire。这是我目前的代码;

Public Sub PopulateGrid()


    Dim AvailableHuts As New DataTable


    AvailableHuts = GetData()


    gvResults.DataSource = AvailableHuts
    gvResults.DataBind()
    gvResults.Enabled = True

    'If statement controlling the enabling and disabling of the Beach Hut booking button

    If Session("LoginID") = "" Then
        For Each rowItem As GridViewRow In gvResults.Rows
            rowItem.Cells(5).Enabled = True
        Next
    End If

    lblCaption.Text = "Your search returned " + CStr(AvailableHuts.Rows.Count) + " results"

End Sub

目前两个按钮都已启用,我不确定需要添加/更改哪些按钮才能获得所需的结果。

根据@Andrei发布的答案,我已将以下内容添加到此Sub;

Protected Sub gvResults_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvResults.RowDataBound
    Dim URL(), Text() As String
    Dim img As Image
    Dim lbl As Label
    Dim FacilitiesImg As PlaceHolder = e.Row.FindControl("imgHolder")
    Dim Infolbl As PlaceHolder = e.Row.FindControl("lblHolder")

     'Added Code from answer
    Dim hireBtn As Button = CType(e.Row.FindControl("btnHire"), Button)
    hireBtn.Visible = Not String.IsNullOrEmpty(Session("LoginID"))

     'Added Code from answer
    Dim LoginBtn As Button = CType(e.Row.FindControl("ButtonLogin"), Button)
    LoginBtn.Visible = String.IsNullOrEmpty(Session("LoginID"))

    If e.Row.RowType = DataControlRowType.DataRow Then

        URL = e.Row.DataItem(3).Split(",")
        Text = e.Row.DataItem(2).Split(",")

        'Add the Facilities Images to the grid Row
        For Each item In URL
            If item.ToString <> "" Then
                img = New Image
                img.ImageUrl = item.ToString
                FacilitiesImg.Controls.Add(img)
            End If
        Next

        'Add the information to the grid row
        'convert # into a carriage return and * into £
        For Each item In Text
            If item.ToString <> "" Then
                lbl = New Label
                lbl.Text = Replace(Replace(item.ToString, "#", "<br />"), "*", "&pound;")
                Infolbl.Controls.Add(lbl)
            End If
        Next

    End If



End Sub

但是,我在尝试运行程序时收到以下错误;

  

如果没有类的显式实例,则无法在共享方法或共享成员初始值设定项中引用类的实例成员

非常感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:1)

不确定您的身份验证是如何实现的,但我们假设您使用HttpContext.Current.Request.IsAuthenticated。然后你可以在后面的代码中操作按钮的Visible属性:

btnHire.Visible = HttpContext.Current.Request.IsAuthenticated
ButtonLoggedIn.Visible = Not HttpContext.Current.Request.IsAuthenticated

<强>更新 对不起,不知何故从帖子中错过了您似乎正在使用Session("LoginID")来决定是否已登录使用的事实。您可以这样做:

btnHire.Visible = Not String.IsNullOrEmpty(Session("LoginID"))
ButtonLoggedIn.Visible = String.IsNullOrEmpty(Session("LoginID"))

更新2。 事实证明,按钮是GridView行的一部分。由于它们位于模板中,因此无法在后面的代码中实现它们。所以你可以选择两种方式。

首先,您可以订阅RowDataBound事件(您已经做过),并在其中执行当前行中的FindControl以找到必要的按钮:

Sub gvResults_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ...
    Button hireBtn = CType(e.Row.FindControl("btnHire"), Button);
    hireBtn.Visible = Not String.IsNullOrEmpty(Session("LoginID"))

同样适用于第二个按钮。

其次,您可以尝试在标记中执行此操作:

<asp:Button ID="btnHire" CommandArgument='<%# Eval("BeachHutID") %>'
            runat="server" Text="Hire Beach Hut" OnClick="Hire_Click" 
            Visible=<%# String.IsNullOrEmpty(Session("LoginID")) %> />

答案 1 :(得分:0)

<asp:Button ID="btnHire" CommandArgument='<%# Eval("BeachHutID") %>' runat="server" Visible='<%# islogin()?true:false %>' Text="Hire Beach Hut" OnClick="Hire_Click" />

<asp:Button ID="ButtonLoggedIn" CommandArgument='<%# Eval("BeachHutID") %>' runat="server" Visible='<%# islogin()?false:true %>' Text="Login to Hire Beach Hut" OnClick="Login_Redirect" />

将此代码粘贴到aspx.cs页面:

Public Shared Function isLogin() As Boolean
    Dim stat As Boolean = False
    Try
        If Session("LoginID") IsNot Nothing AndAlso Session("LoginID") <> "" Then
            stat = True
        End If
    Catch e1 As Exception
        stat = False
    End Try
    Return stat
End Function

EDIT2:

Visible='<%# islogin()?false:true %>这意味着如果isLogin()返回true,则Visible属性设置为false,否则visible属性设置为true }。