很简单的方法,就是用js实现:
页面:
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False">
后台代码:
private void dgUserList_PreRender(object sender, System.EventArgs e)
{
foreach (DataGridItem item in dgUserList.Controls[0].Controls)
{
if (item.ItemType == ListItemType.Header)
{
CheckBox chkAll=(CheckBox)item.FindControl("chkAll");
System.Text.StringBuilder strScript = new System.Text.StringBuilder("
for(int i=0; i{
strScript.Append(" document.all('" + dgUserList.Items[i].Cells[0].FindControl("chkItem").ClientID + "').checked = bAll; n");
}
strScript.Append(" } n");
strScript.Append(" n");
if(!Page.IsClientScriptBlockRegistered("checkStatus"))
Page.RegisterClientScriptBlock("checkStatus",strScript.ToString());
chkAll.Attributes.Add("onclick","checkStatus()");
return;
}
}
方法2
(一).功能
1. JavaScript检索CheckBox并实现全选和全消功能
用C#等写的CheckBox需要回发到服务端执行,
而用JavaScript可以在直接客户端实现,效率高些
(二).代码
1. DataGrid中的代码主要片段:
//头模板代码
οnclick="javascript:SelectAll(this);">
//项模板代码
2. 在当页加入:
var theBox=tempControl;
xState=theBox.checked;
elem=theBox.form.elements;
for(i=0;i if(elem[i].type=="checkbox" && elem[i].id!=theBox.id)
{
if(elem[i].checked!=xState)
elem[i].click();
}
}
3.当使用者选择好后台代码取得某列CheckBox的值:
for (int i = 0;i {
bool blnIfSelect = ((CheckBox)this.DataGrid.Items[i].FindControl("chkItem")).Checked;
....; //这是可以根据blnIfSelect进行各种操作了
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/631872/viewspace-911218/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/631872/viewspace-911218/