Gridview多次删除

时间:2011-03-07 17:12:34

标签: asp.net vb.net gridview

我正在尝试从GridView中删除多行,但我很难找出是否选中了复选框。

目前我的代码没有尝试删除任何内容,只需检查哪些复选框已选中,哪些复选框未选中。我的尝试没有显示任何复选框被检查,并且似乎循环通过GridView行两次!

.ASPX

<asp:GridView ID="gvImages" DataKeyNames="id" runat="server" AutoGenerateColumns="False" BorderWidth="0px" GridLines="None">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="imageId" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ImageField DataImageUrlField="image_path" DataImageUrlFormatString="~/admin/images/{0}"></asp:ImageField>
            <asp:BoundField DataField="id" />
        </Columns>
    </asp:GridView>
    <asp:Button
       ID="btnMultipleRowDelete"
       OnClick="btnMultipleRowDelete_Click"
       runat="server"
       Text="Delete Rows" />

代码背后

    Protected Sub btnMultipleRowDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMultipleRowDelete.Click

    ' Looping through all the rows in the GridView
    For Each row As GridViewRow In gvImages.Rows

        Dim checkbox As CheckBox = CType(row.FindControl("imageId"), CheckBox)
        Dim rowID As Integer = Convert.ToInt32(gvImages.DataKeys(row.RowIndex).Value)

        'Check if the checkbox is checked.
        If checkbox.Checked Then

            Response.Write("Deleted" & rowID & "<br />")

        Else

            Response.Write("Not deleted: " & rowID & "<br />")

        End If

    Next row

End Sub

感谢您的帮助。学家

1 个答案:

答案 0 :(得分:1)

您是否在Page_Load上绑定了GridView?

确保绑定周围有If Not Page.IsPostBack Then

If Not Page.IsPostBack Then
  BindGridViewHere
End If

否则,当按钮被触发时,gridview将被反弹,这意味着所有复选框都被重置!