首先,需要定义一个主键列,用于当选中此行记录时的标识,可以用visible="true"让它隐藏
![](/Images/OutliningIndicators/None.gif)
一、在datagrid控件中添加一模板列,此模板列用于选择要删除的所有item
二、在此模板列<itemTemplate>项中加入一asp.net控件 asp:CheckBox
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
三、在此模板列<HeaderTemplate>项中加入html控件 checkbox,添加一客户端事件onclick="checkAll()"
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
四、添加checkAll函数,作用是当选择标题的复选框时,选中datagrid中所有的复选框列表项
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
五、在此模板<FooterTemplate>项中添加一个服务端button,单击此按钮就执行相应的操作
![](/Images/OutliningIndicators/None.gif)
protected void lbntDelAll_OnClick(object sender,System.EventArgs e)
{
System.Text.StringBuilder dels=new System.Text.StringBuilder("");//要删除的所有主键
bool first=true;
foreach(DataGridItem item in dgData.Items) //遍历所有item找出所有要删除的主键
{
switch(item.ItemType )
{
case ListItemType.Footer :
break;
case ListItemType.Header :
break;
default:
CheckBox chk=(CheckBox)item.Cells[1].FindControl("chkRow");
if(chk.Checked)
{
if(first)
{
first=false;
}
else
{
dels.Append (",");
}
dels.Append("'"+item.Cells[0].Text +"'");
}
break;
}
}
if(dels.ToString()!="")
{
string msg;
bool b=_m.Delete(dels.ToString(),out msg); //执行具体操作
}