2017-02-11 50 views
0

我使用sqldatasource來顯示和刪除Oracle表中的數據,數據可以顯示在gridview和detailsview控件,但不能刪除,當我點擊刪除鏈接按鈕在細節的仰視圖,看我下面的網頁代碼:刪除鏈接按鈕不在detailsview中的asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Master.aspx.cs" Inherits="HarnessMaintain.Pages.Master" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" 
      DataSourceID="SqlDataSource1"> 
      <Columns> 
       <asp:CommandField ShowSelectButton="True" /> 
       <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> 
       <asp:BoundField DataField="MODE_TYPE" HeaderText="MODE_TYPE" SortExpression="MODE_TYPE" /> 
       <asp:BoundField DataField="PARTNER_NAME" HeaderText="PARTNER_NAME" SortExpression="PARTNER_NAME" /> 
      </Columns> 
     </asp:GridView> 
     <br /> 
     <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource2" 
      OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted" 
      OnItemUpdated="DetailsView1_ItemUpdated"> 
      <Fields> 
       <asp:CommandField ShowDeleteButton="True" /> 
      </Fields> 
     </asp:DetailsView> 
     <br /> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT &quot;ID&quot;, &quot;MODE_TYPE&quot;, &quot;PARTNER_NAME&quot; FROM &quot;MSWSCEMASTER&quot; ORDER BY &quot;ID&quot;"> 
     </asp:SqlDataSource> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
      DeleteCommand="DELETE FROM &quot;MSWSCEMASTER&quot; WHERE &quot;ID&quot; =:ID" 
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM &quot;MSWSCEMASTER&quot; WHERE (&quot;ID&quot; = :ID)"> 
      <DeleteParameters> 
       <asp:Parameter Name=":ID" Type="Decimal" /> 
      </DeleteParameters> 
      <SelectParameters> 
       <asp:ControlParameter ControlID="GridView1" Name="ID" PropertyName="SelectedValue" 
        Type="Decimal" /> 
      </SelectParameters> 
     </asp:SqlDataSource> 
    </div> 
    </form> 
</body> 
</html> 

,但是當我點擊刪除鏈接按鈕,數據不能被刪除,我不知道爲什麼,有誰能夠告訴我的原因或如何解決這個問題?

enter image description here

+0

是什麼讓你認爲數據沒有被刪除?當你點擊刪除鏈接時是否會拋出任何錯誤? –

+0

沒有任何錯誤 – user2575502

+0

您是否嘗試過我在下面提供的解決方案? –

回答

0

你需要設置DetailsView控件的DataKeyNames屬性,使得DeteteParameter會從那裏得到的參數值。

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DataSourceID="SqlDataSource2" DataKeyNames="ID" 
     OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted" 
     OnItemUpdated="DetailsView1_ItemUpdated"> 
    <Fields> 
     <asp:CommandField ShowDeleteButton="True" /> 
    </Fields> 
</asp:DetailsView>