【问题标题】:Gridview Delete Button in ASP.NETASP.NET 中的 Gridview 删除按钮
【发布时间】:2011-02-11 13:18:05
【问题描述】:

我刚刚问了一个关于这个主题的问题; All Row has a Delete Button in Gridview

我有一张简单的桌子AVUKAT

列 --> HESAPMUSTERIAVUKAT

我在 Gridview 中显示数据。

我激活AutoGenerateDeleteButton

但是当我单击一行的删除按钮时,我收到这样的错误。

Server Error in '/' Application.
Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: Deleting is not supported by data source 'GridviewDataSource' unless DeleteCommand is specified.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

单击删除按钮时会激活哪个功能?

这个函数的代码应该是什么?

【问题讨论】:

  • Soner 你写过删除代码吗?或者你只是点击这个链接?
  • 不,我不写。只是我问我怎么写这段代码:)

标签: c# .net asp.net gridview delete-row


【解决方案1】:

看看这篇 MSDN 文章:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.deletecommand.aspx#Y725

它描述了设置 SQLDataSource 以从数据库中删除数据。

更新

您的 SQLDataSource 缺少“删除参数”(MSDN Link)。

更新您的 SQLDataSource 以包含 DeleteParameters,如下所示(您需要更新 ControlParameters 的 ConnectionString 和 ControlID 等内容):

<asp:SqlDataSource
   id="SqlDataSource1"
   runat="server"
   ConnectionString="myConnectionString"
   SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]" 
   DeleteCommand="DELETE FROM [AVUKAT] WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP">
   <DeleteParameters>
      <asp:ControlParameter Name="MUSTERI" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="AVUKAT" ControlId="DropDownListID" PropertyName="SelectedValue" />
      <asp:ControlParameter Name="HESAP" ControlId="DropDownListID" PropertyName="SelectedValue" />
   </DeleteParameters>
</asp:SqlDataSource>

【讨论】:

  • 但是我的gridview 已经有一个数据源了。如何在这个 gridview 中添加另一个数据源?
  • 您当前在数据源上输入了什么作为 DeleteCommand?
  • SelectCommand="SELECT * FROM [AVUKAT] ORDER BY [MUSTERI]" DeleteCommand="DELETE FROM [AVUKAT] WHERE MUSTERI = @MUSTERI AND AVUKAT = @AVUKAT AND HESAP = @HESAP"> 不是'对吗?
【解决方案2】:

除此之外,请记住在您将运行此删除命令的表中使用主键。因为在没有主键的情况下,删除命令将抛出异常,因为如果多行具有相同的值,生成的查询可能会不明确 (事实上​​,除非您的表有主键,否则您将无法附加内置的 DELETE 命令。)

【讨论】:

    【解决方案3】:

    您需要按照this tutorial 中的说明实现 RowDeleting 事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-28
      • 2011-01-28
      • 1970-01-01
      相关资源
      最近更新 更多