1

我试图在从数据库中删除之前保存一些信息。我正在使用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>
4

2 回答 2

2

声明eSystem.Web.UI.WebControls.GridViewDeleteEventArgs。然后,您可以使用 获取行索引e.RowIndex

于 2013-04-02T18:16:28.670 回答
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 

注意:确保您仍然有 Row_deleting 事件处理程序方法,但是 Empty 没有任何代码。否则它会给你错误。

于 2013-04-02T18:22:40.247 回答