为什么删除按钮不显示?

时间:2014-10-28 20:32:32

标签: asp.net webforms

<asp:DetailsView Width="750" runat="server" ID="ctlApplicationEdit" DefaultMode="Edit" DataSourceID="applicationDataSourceDetails" DataKeyNames="ID" AutoGenerateRows="false" CssClass="EditApplicationWindow" OnModeChanged="OnApplicationChanged">
  <Fields>

<!-- Does not work as expected: (Delete button is not shown) -->

      <asp:CommandField ButtonType="Button" ShowEditButton="true" ShowDeleteButton="true" /> 

<!-- Works as expected: (Delete button is shown and calls the delete method when pressed)-->

      <asp:TemplateField HeaderText="Delete">
          <ItemTemplate>
              <asp:Button ID="deleteButton" runat="server" CommandName="Delete" Text="Delete"/>
          </ItemTemplate>
      </asp:TemplateField>

  </Fields>

</asp:DetailsView>

我在ObjectDataSource上设置了DeleteMethod,这可以从TemplateField按预期工作。

我的问题是:为什么命令字段只显示取消和更新按钮,但不显示删除?在什么情况下会发生这种情况?

1 个答案:

答案 0 :(得分:0)

看起来你必须告诉DetailsView自动生成删除按钮。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.autogeneratedeletebutton(v=vs.110).aspx

<asp:DetailsView AutoGenerateDeleteButton="True|False" />

当AutoGenerateDeleteButton属性设置为true时,带有“删除”按钮的CommandField行字段将自动显示在DetailsView控件中。单击“删除”按钮将永久删除数据源中的该记录。