DataList同時選定刪除多項

來源:互聯網
上載者:User

執行效果:DataList項目範本裡放上CheckBox和需要顯示的資料,讓選定一系列的CheckBox後,單擊全部刪除按鈕可實現同時刪除選定的項,並作出是否刪除有資料的判斷.

全部刪除按鈕放在DataList的項目範本以外,DataList的DataKeyField設定為每項的主鍵.

bool flag = false;  //定義一個是否刪除了資料的標記
foreach (DataListItem dlt in DataList1.Items)  //迴圈取出DataList裡的每一項
{
        //用DataListItem的FindControl方法找到CheckBox,參數為CheckBox的ID,返回為Control類型,將其轉換成CheckBox
        CheckBox ckb = (CheckBox)dlt.FindControl("cbDelete"); 
        if (ckb.Checked)  //判斷CheckBox是否選中
    {
                int id = Convert.ToInt32(DataList1.DataKeys[dlt.ItemIndex]);  //取出DataList該項的主鍵,用於資料庫刪除操作
        PostOperate po = new PostOperate();  //執行個體化封裝的資料操作類
        flag = po.Delete(id);  //執行類中的Delete方法刪除資料,刪除成功則返回True,如果沒有刪除資料,flag為false
        }
}
if (flag)  //根據flag的值判斷是否刪除有資料
{
        JS.AlertAndReturn(this.Page, "文章刪除成功!", "postmanager.aspx");
}
else
{
        JS.AlertAndReturn(this.Page, "沒有選定要刪除的項!", "postmanager.aspx");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.