我想在从数据库中删除之前保存一些信息。我正在使用asp gridview。我正在尝试使用RowDeleting事件从正在删除的行中检索值。但是,我一直都不成功。
Protected Sub gv1_RowDeleting(sender As Object, e As System.EventArgs) Handles gv1.RowDeleting
Dim a As String = gv1.Columns(0).ToString
Dim b As String = gv1.SelectedRow.RowIndex.ToString
Dim c As String = gv1.Rows(a).Cells(0).Text
End Sub
a最终成为标题行的第一列。 b出错了。我尝试了很多不同的东西,无法想出一个行索引。我本以为这个事件只能访问被删除的行。这是我的gridview声明:
<asp:GridView ID="gv1" runat="server" AllowSorting="True" AutoGenerateColumns="False" EnableViewState="false"
DataSourceID="ds1" EmptyDataText="NO ROWS FOUND" CssClass="gridView" DataKeyNames="id" GridLines="None">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" SortExpression="id" />
<asp:BoundField DataField="totalsd" HeaderText="SD" SortExpression="totalsd" />
<asp:BoundField DataField="freesd" HeaderText="FREE" SortExpression="freesd" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete" OnClientClick="return confirm('Are you sure you want to delete this record?');" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:2)
将e
声明为System.Web.UI.WebControls.GridViewDeleteEventArgs
。然后,您可以使用e.RowIndex
获取行索引。
答案 1 :(得分:1)
尝试使用此示例方式,例如使用RowCommand
<asp:LinkButton ID="DelButton" runat=server Text="Delete" CommandName="Delete" CausesValidation=False CommandArgument='<%# Databinder.Eval(Container,"DataItem.id") %>'></asp:LinkButton>
并在vb文件中更改
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Dim MyConn1 As New OleDbConnection(Your Connection string)
Dim cmdDelete As OleDbCommand
If e.CommandName = "Delete" Then
cmdDelete = New OleDbCommand("Delete from table" & _
"Where id= " & e.CommandArgument.ToString() & ";", MyConn1)
MyConn1.Open()
cmdDelete.ExecuteNonQuery()
MyConn1.Close()
BindAutoMake()
End If
End Sub
N.B:确保您仍然拥有Row_deleting事件处理程序方法,但没有任何代码而为Empty。否则会给你错误。