ObjectDataSource的刪除用法

來源:互聯網
上載者:User

原標題:ObjectDataSource用法之六(刪除)

1.  准備條件

參數:ObjectDataSource用法之一(SelectMethod來進行簡單的邦定)

2.  在業務處理類中添加如下方法

public void DeleteItem(string UID)
{
    HttpContext.Current.Response.Write("UID = " + UID + "<br/>");
}

3. aspx頁面內容為

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="AllItems" DeleteMethod="DeleteItem" OldValuesParameterFormatString="{0}" TypeName="Member">
    <DeleteParameters>
        <asp:Parameter Name="UID" />
    </DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" AutoGenerateDeleteButton="true" DataKeyNames="UID">
</asp:GridView>

說明: 參考ObjectDataSource用法之五(更新) 

此外有兩點需要在使用刪除的時候注意:
1,在ConflictDetection=OverwriteChanges的時候,使用ObjectDataSource的DeleteMethod必須要在GridView上聲明DataKeyNames,因為在沒有設定DataObjectType的時候,ObjectDataSource只會給DeleteMethod傳遞一個參數,例如:OldValuesParameterFormatString="old_{0}"且DataKeyNames="id",那麼它只會把id列的原始值old_id作為參數傳遞給背景DeleteMehod,所以如果不設定DataKeyNames,DeleteMethod不會接受到任何值,也就無法進行刪除操作!而在設定了DataObjectType的時候,只有設定了DataKeyNames,ObjectDataSource才會把各個Detele的Parameter的值傳遞給DeleteMethod的實體參數的屬性,否則你會發現DeleteMethod的實體參數的所有屬性都為空白或定義時候的預設值。

2,在ConflictDetection=CompareAllValues的時候,不必設定DataKeyNames,因為就算不設定DataKeyNames,GridView還是會將可編輯列的原始值全部傳入DeleteMethod的函數參數或函數參數的實體屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.