如何在单击网格视图外部的其他按钮时验证网格视图中已批准的所有按钮

How to verify all button in approved in gridview while click on other button that is outside the gridview

我想在 vb.net 中添加一个按钮,例如在另一个 table 中的 gridview 外批量批准。当点击那个按钮时,即是批量批准。所有验证按钮都在每一行的 gridview 中进行验证...并更改为已验证。

好的,所以我们有某种网格,每行都有某种批准按钮。

好的,所以我们有一些像这样的典型标记:

这是一个网格(酒店预订),然后“管理员”必须批准每一个。

所以,像这样说一些标记 - 没什么特别的:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID" CssClass="table">
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"  />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName" ItemStyle-Width="180" />
                <asp:BoundField DataField="City" HeaderText="City"  />
                <asp:BoundField DataField="Description" HeaderText="Description"  />
                <asp:TemplateField HeaderText="Approved" ItemStyle-HorizontalAlign="Center" >
                    <ItemTemplate>
                           <asp:CheckBox ID="chkApproved" runat="server" Checked='<%# Eval("Approved") %>' 
                                CssClass="bigcheck"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="By">
                    <ItemTemplate>
                        <asp:Label ID="lblApBy" runat="server" Text='<%# Eval("ApprovedBy") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>                    
                <asp:TemplateField HeaderText="Approve">
                    <ItemTemplate>
                        <asp:Button ID="cmdApprove" runat="server" Text="Approve" CssClass="btn"
                            onclick="cmdApprove_Click"/>
                    </ItemTemplate>
                </asp:TemplateField>


            </Columns>
        </asp:GridView>
        <div style="float:right">
        <asp:Button ID="cmdApproveAll" runat="server" Text="Approve All" CssClass="btn" />

我们加载网格的代码:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        LoadGrid()
    End If

End Sub

Sub LoadGrid()

    Using conn As New SqlConnection(My.Settings.TEST4)

        Dim strSQL As String = "SELECT TOP 6 * from tblHotels"
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            Dim rstData As New DataTable
            rstData.Load(cmdSQL.ExecuteReader)
            GridView1.DataSource = rstData
            GridView1.DataBind()
        End Using
    End Using

End Sub

我们现在有这个:

好的,所以我们在每一行都有一个飞机简按钮。批准一行的代码是这个代码:

(网格视图中的一个简单标准按钮 - 按钮点击)

Protected Sub cmdApprove_Click(sender As Object, e As EventArgs)

    ' approve one row
    Dim btn As Button = sender
    Dim gRow As GridViewRow = btn.NamingContainer

    Call ApproveGRow(gRow)

End Sub

Sub ApproveGRow(gRow As GridViewRow)

    ' set checkbox = checked
    Dim ckApprove As CheckBox = gRow.FindControl("chkApproved")
    ckApprove.Checked = True
    Dim lBy As Label = gRow.FindControl("lblApBy")
    lBy.Text = UserInitials

    ' Get data base pk id

    Dim pkID As Integer
    pkID = GridView1.DataKeys(gRow.RowIndex).Item("ID")

    ' Now udpate database
    Dim strSQL As String =
        "UPDATE tblHotels SET Approved = 1, ApprovedBy = @Initials WHERE ID = @ID"

    Using conn As New SqlConnection(My.Settings.TEST4)
        Using cmdSQL As New SqlCommand(strSQL, conn)
            conn.Open()
            cmdSQL.Parameters.Add("@Initials", SqlDbType.NVarChar).Value = UserInitials
            cmdSQL.Parameters.Add("@ID", SqlDbType.Int).Value = pkID
            cmdSQL.ExecuteNonQuery()
        End Using
    End Using

End Sub

因此,在单击按钮时,我们获得当前行的单击,然后调用我们接受网格 ro

的批准例程

所以,如果我点击第二行的 say,我们会看到:

但是,如何一键完成整个网格并批准所有行?

好吧,全部批准按钮如下所示:

Protected Sub cmdApproveAll_Click(sender As Object, e As EventArgs) Handles cmdApproveAll.Click

    For Each gRow In GridView1.Rows

        Call ApproveGRow(gRow)

    Next

End Sub

如果我点击它,我会得到这个: