2010-03-02 57 views
0

我正在使用gridview來選擇,刪除和更新數據庫中的數據。我已經寫了一個SP來完成所有這些操作。基於參數SP決定執行哪個操作。如何從使用數據源和SP使用嚮導的gridview刪除數據

這裏是我的GridView image of my grid http://www.freeimagehosting.net/uploads/0a5de50661.jpg

  <asp:GridView ID="GridView1" runat="server" DataSourceID="dsDomain" 
      AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
      DataKeyNames="DomainId" > 
      <Columns> 
       <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
       <asp:BoundField DataField="DomainId" HeaderText="DomainId" 
        InsertVisible="False" ReadOnly="True" SortExpression="DomainId"> 
       </asp:BoundField> 
       <asp:BoundField DataField="Domain" HeaderText="Domain" 
        SortExpression="Domain"> 
       </asp:BoundField> 
       <asp:BoundField DataField="Description" HeaderText="Description" 
        SortExpression="Description" > 
       </asp:BoundField>        
       <asp:BoundField DataField="InsertionDate" HeaderText="InsertionDate" 
        SortExpression="InsertionDate"> 
       </asp:BoundField>        
     </asp:GridView> 

數據源,我使用的圖像是在這裏

  <asp:SqlDataSource ID="dsDomain" runat="server" 
       ConnectionString="<%$ ConnectionStrings:conLogin %>" 
       SelectCommand="Tags.spOnlineTest_Domain" 
       SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False" 
         DeleteCommand="Tags.spOnlineTest_Domain" DeleteCommandType="StoredProcedure" OnDeleting="DomainDeleting"> 

       <SelectParameters> 

        <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="DomainId" Type="String" /> 
        <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" /> 
        <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" /> 
        <asp:Parameter DefaultValue="1" Name="OperationType" Type="Byte" /> 
       </SelectParameters> 
       <DeleteParameters> 
        <asp:ControlParameter ControlID="GridView1" Name="DomainId" 
         PropertyName="SelectedValue" Size="4" Type="Int32" /> 
    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" /> 
        <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" /> 
        <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="4" Name="OperationType" Type="Byte" /> 
       </DeleteParameters> 
      </asp:SqlDataSource> 

選擇操作工作正常。 但是,當我試圖刪除,它說

過程或函數「spOnlineTest_Domain」需要參數「@域」,但未提供
但我提供這種參數,因爲

我的存儲過程調用是這樣

EXEC Tags.spOnlineTest_Domain NULL,NULL,NULL,1 //對於選擇最後一個參數爲1 EXEC Tags.spOnlineTest_Domain「S我們的過程有4個參數,其中最後一個參數將由程序員設置,它將告訴程序需要執行什麼樣的操作。 僅供選擇最後一個參數必須爲空。 對於刪除第一個和最後一個參數不能爲NULL。

我的第一個刪除參數是表的主鍵。我傳遞這個值,當用戶選擇一行並點擊刪除。我不知道通過使用PropertyName =「SelectedValue」,我會得到正確的ID值。

http://www.freeimagehosting.net/uploads/0a5de50661.jpg />

+0

有兩個問題:你在GrdiView中定義了「DataKey」嗎?你使用的是ObjectDataSource嗎?如果是的話爲什麼不跟蹤它? – Mostafa 2010-03-02 15:20:09

+0

是的,我已經定義了DataKeyNames =「DomainId」。是的,我得到一個堆棧跟蹤。不能理解它 – 2010-03-02 15:39:36

+0

請發佈您的objectdatasource標記 – madatanic 2010-03-02 15:49:14

回答

2

如果您還沒有實現ObjectDataSource控件的OnDeleting事件,請嘗試以下

<asp:ObjectDataSource .... OnDeleting="YourDeletingEvent" ... 
</asp:ObjectDataSource> 

在後面的代碼:

private void YourDeletingEvent(object source, ObjectDataSourceMethodEventArgs e) 
{ 
    IDictionary paramsFromPage = e.InputParameters; 

    //In this case I assume your stored procedure is taking a DomainId as a parameter 
    paramsFromPage.Remove("Domain"); 
    paramsFromPage.Add("Domain", (int)paramsFromPage["DomainId"]); 
    paramsFromPage.Remove("DomainId"); 
} 

請看here瞭解更多詳情。

+0

@madatanic這是問題中的修訂。 Plz檢查它 – 2010-03-02 18:31:17

+0

好吧,然後將域添加到您gridview的DataKeyNames屬性中 – madatanic 2010-03-02 19:40:05

+0

爲什麼必須將域添加到datakeynames中? – 2010-03-02 19:58:29

1

u能寫:(之間GRIDVIEW標籤) \ < ASP:的TemplateField ShowHeader = 「假」> \ ASP:LinkBut​​ton的ID = 「linkBut​​ton1」 RUNAT = 「服務器」 的CausesValidation = 「假」 的CommandName =「刪除「 OnClientClick =」return confirm('你確定要刪除這條記錄嗎?「);」文本= 「刪除」/>

 </ItemTemplate> 

    </asp:TemplateField> 

和後u能執行 「GridView1_RowDeleting」 梅索德,像: 保護無效GridView1_RowDeleting(對象發件人,GridViewDeleteEventArgs E) {

 //makeDelete("YourStoredProcedure"); 
     //mydataBindGV(); 
    } 
+0

感到抱歉\ <和\ \但我無法修復它們 – liliane 2011-07-14 09:01:51

相關問題