我使用GridView和SqlDataSource,用于选择、更新和删除,以及针对插入E 210的DetailsView,问题是,当我单击这些按钮之一时,就会得到以下错误:Delete顺序(以及与Insert和Update相同的错误):
数据源“SqlDataSource1”不支持删除,除非指定了DeleteCommand。
自己添加这些语句非常累人,在某些情况下,我需要定义存储过程。我之所以问这个问题,是因为我读到了一个教程(不幸的是,我不记得在哪里),Visual会自动生成这些教程。
源代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Donor.aspx.cs" Inherits="Donor" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="Id" DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
ForeColor="#333333" GridLines="None" AllowSorting="True">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ItemStyle-Width="120px"/>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-Width="150px"/>
<asp:BoundField DataField="BloodGroup" HeaderText="BloodGroup" SortExpression="BloodGroup" ItemStyle-Width="120px"/>
<asp:BoundField DataField="Disease" HeaderText="Disease" SortExpression="Disease" ItemStyle-Width="120px"/>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Name], [BloodGroup], [Disease], [Id] FROM [Patient]">
</asp:SqlDataSource>
<asp:DetailsView ID="DetailsView1" runat="server" CellPadding="4"
DataSourceID="SqlDataSource2" ForeColor="#333333"
GridLines="None" Height="50px" Width="125px"
DefaultMode="Insert">
<AlternatingRowStyle BackColor="White" />
<CommandRowStyle BackColor="#FFFFC0" Font-Bold="True" />
<FieldHeaderStyle BackColor="#FFFF99" Font-Bold="True" />
<Fields>
<asp:CommandField ShowInsertButton="True" />
</Fields>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [Name], [BloodGroup], [Disease] FROM [Patient]" InsertCommand="INSERT INTO [Patient] ([Name],[BloodGroup],[Disease]) VALUES (@Name, @BloodGroup, @Disease)">
<InsertParameters>
<asp:Parameter Name="Name" />
<asp:Parameter Name="BloodGroup" />
<asp:Parameter Name="Disease" />
</InsertParameters>
</asp:SqlDataSource>
</asp:Content>发布于 2015-12-30 12:19:16
将DeleteCommand添加到SqlDataSource中
DeleteCommand="Delete from yourTable WHERE (ID = @ID)"如下所示:
<asp:SqlDataSource runat="server" DeleteCommand="Delete from yourTable WHERE (ID = @ID)">
<DeleteParameters>
<asp:Parameter Name="ID"></asp:Parameter>
</DeleteParameters>
</asp:SqlDataSource>编辑:使ASP.NET无需任何代码就自动生成DELETE命令,执行以下步骤:
SqlDataSource的“任务”窗口中,单击“配置数据源”。https://stackoverflow.com/questions/34529385
复制相似问题