删除gridview中删除所有行的链接

时间:2014-07-28 01:55:00

标签: c# asp.net sql gridview sqldatasource

我的gridview遇到问题,因为删除链接正在删除我要逐个删除的所有行。我已经记录了我的屏幕,可能会更多地关注我的问题

https://www.youtube.com/watch?v=VAh0A-UOafU&feature=youtu.be

  <asp:GridView ID="FavoritGRDSHOW" runat="server" AllowPaging="True" 
       DataKeyNames="FavID" DataSourceID="FavoriteGRDView" 
       PageSize="6" Width="600px">
       <Columns>
          <asp:CommandField ShowDeleteButton="True" ButtonType="Image" 
               DeleteImageUrl="~/iconsimg/Delete2.png" />
          <asp:BoundField DataField="FavID" HeaderText="FavID" 
               InsertVisible="False" ReadOnly="True" SortExpression="FavID" />
          <asp:BoundField DataField="AdsID" HeaderText="AdsID" 
               SortExpression="AdsID" />
          <asp:BoundField DataField="UID" HeaderText="UID" SortExpression="UID" />
          <asp:BoundField DataField="AdsTit" HeaderText="AdsTit" 
               SortExpression="AdsTit" />
       </Columns>
   </asp:GridView>
   <asp:SqlDataSource ID="FavoriteGRDView" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"
        SelectCommand="SELECT [FavID], [AdsID], [UID], [AdsTit] FROM [favourite] 
                        WHERE ([UID] = @UID)"
        OldValuesParameterFormatString="original_{0}" 
        DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = FavID" >
        <SelectParameters>
          <asp:SessionParameter Name="UID" SessionField="UsrNme" Type="String" />
          </SelectParameters>
    </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:3)

您的删除声明:

DELETE FROM [favourite] WHERE [FavID] = FavID

等同于此,因为每条记录都会匹配自己的字段:

DELETE FROM [favourite]   // deletes all rows in the table

您需要指定要删除的ID并将其作为参数传递:

<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"

     DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = @FavID">

     <DeleteParameters>
         <asp:Parameter Name="FavID" Type="Int32" />
     </DeleteParameters>

</asp:SqlDataSource>

你可以read more here

答案 1 :(得分:0)

使用它:

<asp:SqlDataSource ID="FavoriteGRDView" runat="server" ConnectionString="<%$ ConnectionStrings:BeravaConnectionString %>"

     DeleteCommand="DELETE FROM [favourite] WHERE [FavID] = @FavID">

     <DeleteParameters>
         <asp:ControlParameter ControlID="FavoritGRDSHOW" PropertyName="SelectedValue" Name="FavID" Type="Int32" />
     </DeleteParameters>

</asp:SqlDataSource>