多次删除不起作用(asp.net vb)

时间:2014-01-10 09:53:24

标签: asp.net vb.net aspxgridview

代码示例aspx vb背后的代码:

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click

    For Each row As GridViewRow In GridView1.Rows

        Dim checkbox As CheckBox = CType(row.FindControl("chkdelete"), CheckBox)

        If checkbox.Checked Then

            Dim ID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)

            SqlDataSource1.DeleteParameters("ID").DefaultValue = ID.ToString()

            SqlDataSource1.Delete()

        End If

    Next row    

前码:
`

         <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="chkdelete" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
           <asp:BoundField DataField="ID" HeaderText="No" 
               SortExpression="ID"/>
           <asp:BoundField DataField="Name" HeaderText="Name" 
               SortExpression="Name"/>
            <asp:CommandField Headertext="Edit" ShowEditButton="True" />
            <asp:CommandField Headertext="Delete" ShowDeleteButton="True" />
       </Columns>    
   </asp:GridView>
   <asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click"/>`   

我的问题是,为什么我的多次删除仍然无法正常工作?我的代码有什么问题?任何人请帮助..

1 个答案:

答案 0 :(得分:0)

Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
  For Each row As GridViewRow In GridView1.Rows
     Dim checkbox As CheckBox = CType(row.FindControl("chkdelete"), CheckBox)
        If checkbox.Checked Then
           Dim ID As Integer = Convert.ToInt32(GridView1.DataKeys(row.RowIndex).Value)
             Delete(ID)
        End If

  Next row 

End Sub

Public Function Delete(ByRef ID as Integer)
' Your Connection Coding comes here

  SqlDataSource1.DeleteParameters("ID").DefaultValue = ID.ToString()
    SqlDataSource1.Delete()
End Function

试试这个...... !!