日期:2014-05-18  浏览次数:20530 次

Datagrid中有一个模块列为checkbox,如果让该列的列头也加一个checkbox,列头checkbox.checked=true,所有行的checked=true....
如何实现?

------解决方案--------------------
ItemDataBind(....)
{
if(e.Row.RowType=DataControlRowType.Header)
{
CheckBox checkbox=e.Row.FindControl( "checboxID ") as CheckBox;
checkbox.Attributes.Add( "onclick ", "checkAllBox(); ");
}
}
Aspx
<script language= "javascript ">
function checkAllBox()
{
var k=document.getElementsByName( "AllcheckBox ");
for(var i=0;i <k.length;i++)
{
k[i].checked=true;
}
}
</script>
注意模版项checkBox应该用客户端的.<input type= "checkbox " name= "AllcheckBox " vaule= " <%# Eval( "主键 ")%> ">
接收checkBox选种的值this.Request[ "AllcheckBox "]; 会得到pk1,pk2,的数组
------解决方案--------------------
HTML:
---------------------
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DataGrid ID= "DataGrid1 " runat= "server " AutoGenerateColumns= "False "
Style= "position: relative ">
<Columns>
<asp:TemplateColumn HeaderText= "选择 ">
<HeaderTemplate>
<asp:CheckBox ID= "CheckBox2 " runat= "server "
Style= "position: relative " Text= "头模板 " AutoPostBack= "True " OnCheckedChanged= "CheckBox2_CheckedChanged " />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID= "CheckBox1 " runat= "server " Style= "position: relative " />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField= "au_id " HeaderText= "ID "> </asp:BoundColumn>
</Columns>
</asp:DataGrid> </div>
</form>
</body>
</html>