【Wonder原创】GridView—CheckBox全选

本文介绍如何在ASP.NET中实现全选功能,通过一个复选框控制一组复选框的状态。具体实现包括前端HTML及JavaScript代码和后端C#代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前台

ContractedBlock.gifExpandedBlockStart.gif代码
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkALL" runat="server" Text="全选" AutoPostBack="false" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="ck_Select" runat="server" Width="20px" />
<asp:Label ID="D_lrrr" runat="server" Font-Bold="true" ForeColor="Red" Text='<%# (Container.DataItemIndex+1)+"." %>'
Visible="true" Width="20px">
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="50px" />

 

ContractedBlock.gifExpandedBlockStart.gifjavascript
<script language="javascript" type="text/javascript">
function selectAll(ctlName,headchkid)
{
var chkall = document.getElementById(headchkid);
var bool = chkall.checked ;
var ctl = document.getElementById(ctlName);//根据控件的在客户端所呈现的ID获取控件
var checkbox = ctl.getElementsByTagName('input');//获取该控件内标签为input的控件
/*所有Button、TextBox、CheckBox、RadioButton类型的服务器端控件在解释成Html控件后,都为<input type=''./>,通过type区分它们的类型。*/
for(var i=0;i<checkbox.length;i++)
{
if(checkbox[i].type=='checkbox')
{
checkbox[i].checked
= bool ;
}
}
}
</script>

 

 

 

二、后台

 

ContractedBlock.gifExpandedBlockStart.gifRowDataBound
if (e.Row.RowType == DataControlRowType.Header)
{
CheckBox chk
= (CheckBox)e.Row.FindControl("chkALL");
string str = string.Format("javascript:selectAll('{0}','{1}');",gvList.ClientID,chk.ClientID);
chk.Attributes.Add(
"onclick", str);
}

 

 

 

转载于:https://www.cnblogs.com/wonder315/archive/2011/01/18/1938410.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值