在整个 gridview 中尝试 运行 行命令时卡住了

Stuck trying to run a rowcommand across the whole gridview

我需要 运行 跨 gridview 的所有元素的行命令以节省工作人员一些时间,但我无法执行操作。这是触发操作的按钮:

Protected Sub lbMasivo_Click(sender As Object, e As EventArgs) Handles lbMasivo.Click
        Dim args = New CommandEventArgs("GestionarCampoAdicional", "")
        For Each item As GridViewRow In gv1.Rows
            Dim arg = New GridViewCommandEventArgs(item, args)
            gv1_RowCommand(item, arg)
        Next
        ShowMessage(TipoMensaje.MsgError, "Done")
End Sub

然后在原始命令中有这个抛出异常的函数(gv1_rowcommand):

Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)

但是,item 应该是一个 GridViewRow,不是吗?正如您可能猜到的那样,我对 vb 或 asp 并不了解,但我正在努力学习。

然后您需要将代码移出到该行命令的单独例程中。换句话说,我们假设您在网格外有一个按钮,说“检查”全部或其他内容。

那么,行命令中的代码应该做什么?没有这些信息,我们都会迷路。

所以,假设我有 gv,我想要一个“全选”框?

这样说:

<asp:GridView ID="GHotels" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="ID" cssclass="table">
    <Columns>
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName"   />
        <asp:BoundField DataField="City" HeaderText="City"           />
        <asp:BoundField DataField="HotelName" HeaderText="HotelName" />
        <asp:BoundField DataField="Description" HeaderText="Description"  />

        <asp:TemplateField HeaderText="Active" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:CheckBox ID="chkActive" runat="server"
                    Checked='<%# Eval("Active") %>'/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Rating">
            <ItemTemplate>
                <asp:DropDownList ID="cboRating" runat="server"
                    DataValueField="ID"
                    DataTextField="Rating" >
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
        
<div style="float:right">
    <asp:Button ID="cmdCheckAll" runat="server" Text="Make All Active" CssClass="btn" />
</div>

加载 gv 的代码为:

Dim rstCboChoice As DataTable
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()

    rstCboChoice = MyRst("SELECT ID, Rating FROM tblRating ORDER BY ID")
    ' Now load our grid
    GHotels.DataSource = MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName")
    GHotels.DataBind()

End Sub


Protected Sub GHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GHotels.RowDataBound

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim cboRating As DropDownList = e.Row.FindControl("cboRating")
        cboRating.DataSource = rstCboChoice
        cboRating.DataBind()
        cboRating.Items.Insert(0, "Not Selected")
    End If

End Sub

现在我们有了这个:

现在,我们可以勾选每一行。或者我们可以选择网格外的按钮,并处理每一行,这样说:

Protected Sub cmdCheckAll_Click(sender As Object, e As EventArgs) Handles cmdCheckAll.Click

    For Each gRow As GridViewRow In GHotels.Rows

        Dim ckBox As CheckBox = gRow.FindControl("chkActive")
        ckBox.Checked = True

    Next

End Sub

因此,无论您有什么代码或想要 运行 并在每一行中出现,然后将其放入上面的循环中。因此,您没有与我们分享您需要或想要为每一行 运行 进行什么样的处理或代码,但不管该代码到底是什么?你把它放在上面的循环中。