我有一个 ASP 按钮链接,GridView1
它应该在单击时从 db 表中删除一个项目,并且我收到以下错误消息:
Deleting is not supported by data source 'SqlDataSource1' unless DeleteCommand is specified.
这是代码:
VB.NET
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
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>
根据我阅读的内容,您需要为SQLDataSource1
.
在我的情况下我该怎么做?