遗失的星空

导航

【Wonder原创】GridView—CheckBox全选

一、前台

代码
<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" />

 

javascript
<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>

 

 

 

二、后台

 

RowDataBound
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);
}

 

 

 

posted on 2011-01-18 15:58  遗失的星空  阅读(198)  评论(0编辑  收藏  举报