Execution result: Put the checkbox and the data to be displayed in the datalist item template. After selecting a series of checkboxes, click the delete all button to delete the selected items at the same time, determine whether to delete data.
The delete all button is placed in the datalist item template. datakeyfield of datalist is set as the primary key of each item.
Bool flag = false; // defines whether a flag deletes data.
Foreach (datalistitem DLT in datalist1.items) // cyclically retrieve each item in datalist
{
// Use the findcontrol method of datalistitem to find the checkbox. The parameter is the ID of the checkbox. the return value is of the control type and is converted to the checkbox type.
Checkbox ckb = (checkbox) DLT. findcontrol ("cbdelete ");
If (ckb. Checked) // checks whether the checkbox is selected.
{
Int id = convert. toint32 (datalist1.datakeys [DLT. itemindex]); // retrieves the primary key of datalist for database deletion.
Postoperate po = new postoperate (); // instantiate the encapsulated data operation class
Flag = Po. Delete (ID); // The delete method in the execution class to delete data. If the deletion is successful, true is returned. If no data is deleted, the flag is false.
}
}
If (FLAG) // determine whether data is deleted based on the flag value
{
JS. alertandreturn (this. Page, "the article is deleted successfully! "," Postmanager. aspx ");
}
Else
{
JS. alertandreturn (this. Page, "No items to delete are selected! "," Postmanager. aspx ");
}