2011-12-03 320 views
0

我有一些問題傳遞參數到DELETE命令,似乎不能很好地理解它是如何工作的。SQLDataSource DeleteCommand獲取參數

<asp:SqlDataSource ID="sdsPropertyList" 
    runat="server" 
    ProviderName="<%$ appSettings:ProviderName %>" 
    ConnectionString="<%$ appSettings:ConnectionString %>" 
    SelectCommand="selPropertyByAcntID" 
    SelectCommandType="StoredProcedure" 
    OnSelecting="sdsPropertyList_Selecting" 
    OnSelected="sdsPropertyList_Selected" 
    DeleteCommand="delPropertyByPropID" 
    DeleteCommandType="StoredProcedure" 
    OnDeleting="sdsPropertyList_Deleting" 
    OnDeleted="sdsPropertyList_Deleted"> 
    <SelectParameters> 
     <asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" /> 
    </SelectParameters> 
    <DeleteParameters> 
     <asp:Parameter Name="in_acntID" Type="Int32" DefaultValue="0" /> 
     <asp:Parameter Name="in_propID" Type="Int32" DefaultValue="0" /> 
    </DeleteParameters> 
</asp:SqlDataSource> 

<asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList" 
    AutoGenerateColumns="false" CssClass="gvPropList"> 
    <Columns> 
     <asp:BoundField HeaderText="ID" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" /> 
     <asp:BoundField HeaderText="Property" DataField="prop_title" 
      ItemStyle-CssClass="gvPropTitle" > 
     <ItemStyle CssClass="gvPropTitle" /> 
     </asp:BoundField> 
     <asp:BoundField HeaderText="Units" DataField="unitCount" 
      ItemStyle-CssClass="gvUnitCount" > 
     <ItemStyle CssClass="gvUnitCount" /> 
     </asp:BoundField> 
     <asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified" 
      ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" > 
     <ItemStyle CssClass="gvDate" /> 
     </asp:BoundField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
       <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add" CommandArgument='<%# Eval("prop_id") %>'></asp:LinkButton> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
    <HeaderStyle CssClass="headerPropList"/> 
    <RowStyle CssClass="gvPropRow" /> 
</asp:GridView> 

protected void sdsPropertyList_Selecting(object sender, SqlDataSourceSelectingEventArgs e) 
    { 
     int userID = Convert.ToInt32(Page.User.Identity.Name); 
     if (userID != 0) 
      e.Command.Parameters["in_acntID"].Value = userID; 
    } 

protected void sdsPropertyList_Deleting(object sender, SqlDataSourceCommandEventArgs e) 
    { 
     int userID = Convert.ToInt32(Page.User.Identity.Name); 
     if (userID != 0) 
     { 
      e.Command.Parameters["in_acntID"].Value = userID; 
     } 
    } 

SELECT語句很簡單,它需要一個輸入參數userID。 但是,DELETE語句需要2個參數輸入。 in_acntID = userID in_propID =綁定字段數據字段prop_id

我在做什麼錯了?並且如果我在SqlDataSource級別定義了CommandName和CommandArgument,它們應該在ItemTemplate級別傳遞嗎?

我想刪除按鈕來實現以下目標:從表(S)在DB

  • 在GridView
  • UPDATE

    後刪除行

    1. 刪除記錄一些額外的研究,我發現,參數的名稱和綁定字段的HeaderText必須是相同的,以便您的gridview中的值可以被SQL com使用數據源的主角。

      除了最初的select命令,我已經刪除了引用後面的所有代碼。

      現在一切正常。

    回答

    0

    經過一些額外的研究後,我發現參數的名稱和綁定字段的HeaderText必須相同,以便數據源的SQL命令可以使用gridview中的值。

    除了最初的select命令,我已經刪除了引用後面的所有代碼。

    現在一切正常。

    <asp:SqlDataSource ID="sdsPropertyList" 
        runat="server" 
        ProviderName="<%$ appSettings:ProviderName %>" 
        ConnectionString="<%$ appSettings:ConnectionString %>" 
        SelectCommand="selPropertyByAcntID" 
        SelectCommandType="StoredProcedure" 
        OnSelecting="sdsPropertyList_Selecting" 
        DeleteCommand="delPropertyByPropID" 
        DeleteCommandType="StoredProcedure" 
        OnDeleted="sdsPropertyList_Deleted" > 
        <SelectParameters> 
         <asp:Parameter Name="acnt_id" Type="Int32" /> 
        </SelectParameters> 
        <DeleteParameters> 
         <asp:Parameter Name="acnt_id" Type="Int32" /> 
         <asp:Parameter Name="prop_id" Type="Int32" /> 
        </DeleteParameters> 
    </asp:SqlDataSource> 
    <asp:GridView ID="gvProperty" runat="server" DataSourceID="sdsPropertyList" 
        AutoGenerateColumns="false" CssClass="gvPropList" DataKeyNames="acnt_id, prop_id"> 
        <Columns> 
         <asp:BoundField HeaderText="acnt_id" InsertVisible="true" DataField="acnt_id" ReadOnly="true" Visible="False" /> 
         <asp:BoundField HeaderText="prop_id" InsertVisible="true" DataField="prop_id" ReadOnly="true" Visible="False" /> 
         <asp:BoundField HeaderText="Property" DataField="prop_title"> 
         <ItemStyle CssClass="gvPropTitle" /> 
         </asp:BoundField> 
         <asp:BoundField HeaderText="Units" DataField="unitCount" > 
         <ItemStyle CssClass="gvUnitCount" /> 
         </asp:BoundField> 
         <asp:BoundField DataField="prop_lastmodified" HeaderText="Last Modified" 
          ItemStyle-CssClass="gvDate" DataFormatString="{0:M/dd/yyyy hh:mm tt}" > 
         <ItemStyle CssClass="gvDate" /> 
         </asp:BoundField> 
         <asp:BoundField HeaderText="Active" DataField="prop_active"> 
         <ItemStyle CssClass="gvPropActive" /> 
         </asp:BoundField> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:LinkButton ID="lbtnEdit" runat="server" CommandName="EditRecord" Text="Edit"></asp:LinkButton> 
           <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="Delete" Text="Delete"></asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="AddRecord" Text="Add"></asp:LinkButton> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
        <HeaderStyle CssClass="headerPropList"/> 
        <RowStyle CssClass="gvPropRow" /> 
    </asp:GridView> 
    
    0

    例如如果該id在列表框或下拉列表中

    <DeleteParameters> 
           <asp:ControlParameter ControlID="controlname" Name="id" PropertyName="SelectedValue" Type="Int32" /> 
            </DeleteParameters> 
    

    我已經使用上述成功刪除。這可以用於文本框或標籤。如果你正在處理事件,爲什麼不把整個刪除過程帶到偶數處理程序?指定整個SQL設置,包括連接,在那裏執行命令。這種方法也適用於我。

    2

    按照MSDN documentation,你需要在GridView上指定的DataKeyNames:

    「使用DataKeyNames屬性指定代表數據源的主鍵的一個或多個字段,則必須設置DataKeyNames屬性。爲了使GridView控件的自動更新和刪除功能正常工作,這些關鍵字段的值被傳遞給數據源控件,以便指定要更新或刪除的行。

    +0

    我試着添加datakeynames DataKeyNames =「prop_id,acnt_id」,但仍然沒有達到我想要的結果。 當我將值傳遞給datakeynames時,它如何作爲參數傳遞給sdsPropertyList_Deleting命令? – Rick

    +0

    這個答案對我來說是修復的,我看到你的解決方案是由於不同的問題,但是想要提醒人們總是包含所有的數據鍵,如果你想把它們作爲參數包含在內,謝謝你的回答! – SelAromDotNet