ASP.NET 2.0中怎么利用GridView批量刪除數(shù)據(jù),很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鐵山港ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鐵山港網(wǎng)站制作公司第一步:創(chuàng)建批刪除界面
由于我們?cè)诘?2章已經(jīng)創(chuàng)建了一個(gè)批刪除界面,因此我們可以簡(jiǎn)單的將其拷貝到BatchDelete.aspx頁(yè)面。首先,打開BatchData文件夾里的BatchDelete.aspx頁(yè)面,以及EnhancedGridView文件夾里的CheckBoxField.aspx頁(yè)面。在CheckBoxField.aspx頁(yè)面,切換到Source模式,將
圖2:復(fù)制CheckBoxField.aspx頁(yè)面里的聲明代碼
然后,切換到BatchDelete.aspx頁(yè)面的Source模式,將代碼粘貼到
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class BatchData_BatchDelete : System.Web.UI.Page { protected void DeleteSelectedProducts_Click(object sender, EventArgs e) { bool atLeastOneRowDeleted = false; // Iterate through the Products.Rows property foreach (GridViewRow row in Products.Rows) { // Access the CheckBox CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); if (cb != null && cb.Checked) { // Delete row! (Well, not really...) atLeastOneRowDeleted = true; // First, get the ProductID for the selected row int productID = Convert.ToInt32(Products.DataKeys[row.RowIndex].Value); // "Delete" the row DeleteResults.Text += string.Format ("This would have deleted ProductID {0}
", productID); //... To actually delete the product, use ... //ProductsBLL productAPI = new ProductsBLL(); //productAPI.DeleteProduct(productID); //............................................ } } // Show the Label if at least one row was deleted... DeleteResults.Visible = atLeastOneRowDeleted; } private void ToggleCheckState(bool checkState) { // Iterate through the Products.Rows property foreach (GridViewRow row in Products.Rows) { // Access the CheckBox CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); if (cb != null) cb.Checked = checkState; } } protected void CheckAll_Click(object sender, EventArgs e) { ToggleCheckState(true); } protected void UncheckAll_Click(object sender, EventArgs e) { ToggleCheckState(false); } }
完成上述工作后,花幾分鐘在瀏覽器里測(cè)試該頁(yè)面.你應(yīng)該首先看到一個(gè)GridView控件列出了前10個(gè)產(chǎn)品,每行列出了產(chǎn)品的name, category,price以及一個(gè)checkbox. 同時(shí)應(yīng)該有3個(gè)按鈕“Check All”, “Uncheck All”和“Delete Selected Products”.點(diǎn)“Check All”按鈕將會(huì)選中所有的checkboxes;而“Uncheck All”按鈕將釋放所有的
checkboxes;點(diǎn)“Delete Selected Products”的話將顯示一個(gè)消息,列出選中的產(chǎn)品的ProductID值,不過并不會(huì)真的刪除產(chǎn)品.
圖3:CheckBoxField.aspx頁(yè)面的界面搬到了BatchDeleting.aspx頁(yè)面
第二步:在事務(wù)里刪除選中的產(chǎn)品
完成界面后,剩下的事情是更新代碼,以便當(dāng)點(diǎn)擊“Delete Selected Products”按鈕時(shí),使用ProductsBLL class類里的DeleteProductsWithTransaction方法來(lái)刪除選中的產(chǎn)品.該方法是我們?cè)诘?1章《在事務(wù)里對(duì)數(shù)據(jù)庫(kù)修改進(jìn)行封裝》里添加的,它接受一系列的ProductID值,然后在一個(gè)事務(wù)里將刪除對(duì)應(yīng)的ProductID的記錄.
DeleteSelectedProducts按鈕的Click事件目前使用的foreach循環(huán)如下:
// Iterate through the Products.Rows property foreach (GridViewRow row in Products.Rows) { // Access the CheckBox CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); if (cb != null && cb.Checked) { // Delete row! (Well, not really...) atLeastOneRowDeleted = true; // First, get the ProductID for the selected row int productID = Convert.ToInt32(Products.DataKeys[row.RowIndex].Value); // "Delete" the row DeleteResults.Text += string.Format ("This would have deleted ProductID {0}
", productID); //... To actually delete the product, use ... //ProductsBLL productAPI = new ProductsBLL(); //productAPI.DeleteProduct(productID); //............................................ } }
對(duì)每行而言,編程引用ProductSelector CheckBox控件,如果它被選中,從DataKeys collection集獲取該產(chǎn)品的ProductID值,然后更新DeleteResults控件的Text屬性以顯示要?jiǎng)h除該行.
上面的代碼并不會(huì)真的刪除任何的記錄,因?yàn)樵赑roductsBLL class類里我們只是注釋出了如何使用Delete方法。 不過就算實(shí)際地運(yùn)用了這些刪除邏輯,這些代碼雖然可以刪除產(chǎn)品但沒有運(yùn)用原子操作.也就是說,如果按順序?qū)︻^幾個(gè)產(chǎn)品刪除成功,如果接下來(lái)的某個(gè)產(chǎn)品刪除失敗(比如可能是違背里外鍵約束),那么將拋出一個(gè)異常,但是前面的刪除操作并不會(huì)回滾.
為了保證使用原子操作,我們將轉(zhuǎn)為使用ProductsBLLclass類的DeleteProductsWithTransaction method方法.由于該方法接受一系列的ProductID值,
我們首先需要編譯這一系列的值,再將其作為參數(shù)傳遞出去.我們首先創(chuàng)建一個(gè)int類型的List
protected void DeleteSelectedProducts_Click(object sender, EventArgs e) { // Create a List to hold the ProductID values to delete System.Collections.Generic.ListproductIDsToDelete = new System.Collections.Generic.List (); // Iterate through the Products.Rows property foreach (GridViewRow row in Products.Rows) { // Access the CheckBox CheckBox cb = (CheckBox)row.FindControl("ProductSelector"); if (cb != null && cb.Checked) { // Save the ProductID value for deletion // First, get the ProductID for the selected row int productID = Convert.ToInt32(Products.DataKeys[row.RowIndex].Value); // Add it to the List... productIDsToDelete.Add(productID); // Add a confirmation message DeleteResults.Text += string.Format ("ProductID {0} has been deleted
", productID); } } // Call the DeleteProductsWithTransaction method and show the Label // if at least one row was deleted... if (productIDsToDelete.Count > 0) { ProductsBLL productAPI = new ProductsBLL(); productAPI.DeleteProductsWithTransaction(productIDsToDelete); DeleteResults.Visible = true; // Rebind the data to the GridView Products.DataBind(); } }
上述代碼創(chuàng)建了一個(gè)int類型的List
圖4里,我們選擇幾個(gè)產(chǎn)品以刪除;圖5顯示的是點(diǎn)擊“Delete Selected Products”按鈕后的界面.注意,在Label控件里顯示的已經(jīng)刪除的產(chǎn)品的ProductID值,而這些產(chǎn)品已經(jīng)刪除掉了,并沒有出現(xiàn)在GridView控件里.
圖4:選中的產(chǎn)品將被刪除
圖5:被刪除產(chǎn)品的ProductID值出現(xiàn)的GridView下面的Label控件里
注意:為驗(yàn)證DeleteProductsWithTransaction method方法的原子操作,你可以為某個(gè)產(chǎn)品在Order Details表里手動(dòng)添加一個(gè)條目,然后嘗試刪除該產(chǎn)品(當(dāng)然與其它產(chǎn)品一起刪除).這將會(huì)違背外鍵約束,注意對(duì)其它產(chǎn)品的刪除操作是如何回滾的.
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。