做大批量的数据删除时,如果有个全选按钮把多个页面上显示的记录全都选中删除那比一条条的删除要人性化得多,接下来说一说如何实现,其实网上一搜有好多文章都是说如何进行批量删除的,大体上可以分为两大类1:利用JS脚本实现全选。2:在服务器端实现全选
首先来说一说如何利用JS实现全选
往页面上拖一个GridView,设置好数据源,并为GridView添加一个模板列,往模板列里添加一个chekcbox,比如下面的代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_Del" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
接下来往页面上添加一个checkbox控件DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_Del" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:CheckBox ID="chk_JS" runat="server" Text="全选(JS)" onclick="selectAll(this)"/>
并为该控件编写JS脚本如下:
<script type="text/javascript">
//根据传入的checkbox的选中状态设置所有checkbox的选中状态
function selectAll(obj)
{
var allInput = document.getElementsByTagName("input");
//alert(allInput.length);
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
//alert(allInput[i].type);
if(allInput[i].type == "checkbox")
{
allInput[i].checked = obj.checked;
}
}
}
</script>
基本效果就有了,接下来我们在点缀一下。在网页中添加一个button//根据传入的checkbox的选中状态设置所有checkbox的选中状态
function selectAll(obj)
{
var allInput = document.getElementsByTagName("input");
//alert(allInput.length);
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
//alert(allInput[i].type);
if(allInput[i].type == "checkbox")
{
allInput[i].checked = obj.checked;
}
}
}
</script>
<asp:Button ID="btn_DeleteRecords" runat="server" OnClientClick="return judgeSelect();" Text="删除选中记录" />
添加JS脚本
//判断是否选中记录,用户确认删除
function judgeSelect()
{
var result = false;
var allInput = document.getElementsByTagName("input");
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
if(allInput[i].checked)
{
result = true;
break;
}
}
if(!result)
{
alert("请先选则要删除的记录!");
return result;
}
result = confirm("你确认要删除选定的记录吗?");
return result;
}
function judgeSelect()
{
var result = false;
var allInput = document.getElementsByTagName("input");
var loopTime = allInput.length;
for(i = 0;i < loopTime;i++)
{
if(allInput[i].checked)
{
result = true;
break;
}
}
if(!result)
{
alert("请先选则要删除的记录!");
return result;
}
result = confirm("你确认要删除选定的记录吗?");
return result;
}
2通过服务器端代码实现全选
向页面同添加一个CheckBox并将AutoPostBack属性设置成true,并为它添加CheckedChanged事件
<asp:CheckBox ID="chk_Server" runat="server" OnCheckedChanged="chk_Server_CheckedChanged" Text="全选(Server)" AutoPostBack="True" />
事件代码如下
int a = this.GridView1.Rows.Count;
for (int i = 0; i < a; i++)
{
CheckBox chk = (CheckBox)this.GridView1.Rows[i].FindControl("chk_Del");
chk.Checked = this.chk_Server.Checked;
}
for (int i = 0; i < a; i++)
{
CheckBox chk = (CheckBox)this.GridView1.Rows[i].FindControl("chk_Del");
chk.Checked = this.chk_Server.Checked;
}