除非指定了 DeleteCommand,否则数据源“SqlDataSource1”不支持删除

Bri*_*ian 1 vb.net asp.net gridview sqldatasource sql-server-2012

我有一个 ASP 按钮链接,GridView1点击后应该从数据库表中删除一个项目,我收到以下错误消息:

Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified. 
Run Code Online (Sandbox Code Playgroud)

这是代码:

网络

Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand


    If e.CommandName.Equals("Delete") Then


        Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
        Dim rowID As String = e.CommandArgument.ToString()
        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True;")
        Dim cmd As New SqlCommand("DELETE content WHERE content_id=@rowID", conn)

        cmd.Parameters.AddWithValue("@rowID", rowID)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()


    End If

End Sub
Run Code Online (Sandbox Code Playgroud)

ASP

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" ViewStateMode="Enabled">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField DataField="content_name" HeaderText="Content Name" SortExpression="content_name">
            </asp:BoundField>
            <asp:BoundField DataField="content_type" HeaderText="Content Type" SortExpression="content_type">
            </asp:BoundField>
            <asp:buttonfield buttontype="Link" commandname="Delete" text="Delete"> 
             <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
            </asp:buttonfield>
             <asp:TemplateField HeaderText=" ">
            <ItemTemplate>
            <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click" ></asp:LinkButton>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,您需要为SQLDataSource1.

在我的情况下我该怎么做?

tea*_*rop 5

如果您将其更改为类似……CommandName="DeleteRow"或其他内容……然后将其更改为if(e.CommandName == "DeleteRow")您将绕过该问题。