Home>

This article mainly introduces the inclusion of a checkbox column in the gridview control to achieve multiple selection of data.In the event of the batch delete button, one by one is deleted by a for loop.

preface:

In the previous tutorial,We created a batch editing interface with gridview.When a user needs to edit multiple records at once,The batch editing interface is useful.Similarly, when a user needs to delete multiple records at the same time,The technique is also useful.

If you have used a mail system,You should be familiar with this most common batch delete interface:each line in the interface contains a checkbox, and in addition, there is a "delete all checked items" button (Figure 1). This tutorial is relatively short,Because we have completed the general framework in the previous tutorial,Previous Chapter 50Add checkbox for gridview controlWe created a gridview control with a checkboxes column;in Chapter 61,Encapsulating database modifications in transactions》, We created a method in the bll business logic layer,This method uses transactions to delete productid-based records. In this tutorial,We will combine these to create an example that handles batch deletes.

Figure 1:Each row contains a checkbox

Step 1:Create batch delete interface

Since we have created a batch delete interface in Chapter 52,So we can simply copy it to the batchdelete.aspx page. First, open the batchdelete.aspx page in the batchdata folder and the checkboxfield.aspx page in the enhancedgridview folder. On the checkboxfield.aspx page, switch to source mode and copy the code in thetag.

Figure 2:Copy the declaration code from the checkboxfield.aspx page

Then, switch to the source mode of the batchdelete.aspx page, and paste the code into thetag. Similarly, copy the background code in checkboxfield.aspx.cs to batchdelete.aspx.cs. (Specifically Say,Is the click event event of the deletedselectedproducts button, the togglecheckstate method, the clickall event of the checkall and uncheckall buttons). After copying,The background code class of the batchdelete.aspx page should contain the following code:

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}<br />", 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);
 }
}

After completing the above work,Take a few minutes to test the page in your browser.You should first see a gridview control listing the first 10 products, each row listing the product's name, category, price, and a checkbox. There should be 3 buttons "check all", "uncheck all" and "delete selected" products ". Clicking the" check all "button will select all checkboxes;and the" uncheck all "button will release all

checkboxes;clicking "delete selected products" will display a message,Lists the productid value of the selected product, but does not actually delete the product.

Figure 3:The interface of the checkboxfield.aspx page has been moved to the batchdeleting.aspx page

Step 2:Delete the selected products in the transaction

After completing the interface,The remaining thing is to update the code,So that when the "delete selected products" button is clicked, the deleted products with transaction method in the productsbll class is used to delete the selected products.This method is our chapter 61Encapsulating database modifications in transactionsAdded in》It accepts a series of productid values, and then deletes the corresponding productid records in a transaction.

The foreach loop currently used by the click event of the deleteselectedproducts button is as follows:

//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}<br />", productid);
 //... to actually delete the product, use ...
 //productsbll productapi=new productsbll ();
 //productapi.deleteproduct(productid);
 //............................................
 }
}

For each line,Programmatically reference the productselector checkbox control, if it is selected,Get the productid value of the product from the datakeys collection set, and then update the text property of the deletersults control to show that the row is to be deleted.

The above code does not really delete any records,Because in the productsbll class we just commented out how to use the delete method. But even if these deletion logics are actually used,Although these codes can delete products, they do not use atomic operations.That is,If the first few products are successfully deleted in sequence,If the next product deletion fails (for example, it may violate the internal and foreign key constraints), then an exception will be thrown,However, the previous delete operation will not be rolled back.

In order to guarantee the use of atomic operations,We will switch to using the deleteproductswithtransaction method of the productsbllclass class. Since this method accepts a series of productid values,

We first need to compile this series of values,Pass it as a parameter.We first create an instance of listof type int. In the foreach loop we need to add the productid value of the product to list. After the loop ends,The listwill be passed to the deleteproductswithtransaction method method of the productsbll class, and the click event handler of the deleteselectedproducts button is updated with the following code:

protected void deleteselectedproducts_click (object sender, eventargs e)
{
 //create a list to hold the productid values ​​to delete
 system.collections.generic.list<int>productidstodelete =
 new system.collections.generic.list<int>();
 //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<br />", 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 ();
 }
}

The above code creates a list of type int(that is, productidstodelete) and fills it with the productid value,After the foreach loop ends,If at least one product is selected,The deleteproductswithtransaction method method of the productsbll class is called and the list is passed. A label control named deleteresults will also be displayed;The data is re-bound to the gridview (naturally, the records just deleted will not be displayed).

In Figure 4, we select several products to delete;Figure 5 shows the interface after clicking the "delete selected products" button.Note that the productid values ​​of the deleted products displayed in the label control, and these products have been deleted.It does not appear in the gridview control.

Figure 4:The selected product will be deleted

Figure 5:The productid value of the deleted product appears in the label control under the gridview

Note:To verify the atomic operation of the deleteproductswithtransaction method method,You can manually add an entry to the order details table for a product,Then try to delete the product (along with other products, of course). This will violate the foreign key constraint,Note how the deletion of other products is rolled back.

to sum up:

If you create a batch delete interface,We need to create a gridview control with a checkboxes column and a button web control. When the button is clicked,We will delete multiple products as a single atomic operation.In this article, we created an interface that integrates the content of the previous 2 chapters.

In the next post,Let's examine how to create a batch insert interface

Happy programming!

About the Author

  • Previous CI framework simple mail sending class instance
  • Next Sixty-two of operating data in ASPNET 20: GridView batch update data