datagrid|javascript|函數 javascript實現datagrid用戶端checkbox列的全選,反選
作者:未知
來源:網路
/**//* 分頁
參數說明:
prefix:首碼;chkAll:全選框;chkSingle:單選框ID
使用方法:
if(e.Item.ItemType == ListItemType.Header)
{
((CheckBox)e.Item.Cells[1].FindControl("chkAll")).Attributes.Add("onclick","CheckAll('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
}
*/
function CheckAll(prefix,chkAll,chkSingle)
{
var indexChkAll;
if(prefix.length != 0)
{
indexChkAll = prefix + "__ctl2_" + chkAll;
}
else
{
indexChkAll = chkAll;
}
var objChkAll = do***ent.getElementById(indexChkAll);
var tempObj;
for(var i=0;i<do***ent.forms[0].elements.length;i++)
{
tempObj = do***ent.forms[0].elements;
if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
{
tempObj.checked = objChkAll.checked;
}
}
}
/**//* 分頁
參數說明:
prefix:首碼;chkAll:全選框;chkSingle:單選框ID
使用方法:
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
((CheckBox)e.Item.Cells[1].FindControl("chkSingle")).Attributes.Add("onclick","CheckSingle('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
}
*/
function CheckSingle(prefix,chkAll,chkSingle)
{
var indexChkAll;
if(prefix.length != 0)
{
indexChkAll = prefix + "__ctl2_" + chkAll;
}
else
{
indexChkAll = chkAll;
}
var objChkAll = do***ent.getElementById(indexChkAll);
var tempObj;
var allCount = 0;
var checkCount = 0;
for(var i=0;i<do***ent.forms[0].elements.length;i++)
{
tempObj = do***ent.forms[0].elements;
if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
{
if(tempObj.checked)
{
checkCount++;
}
else
{
objChkAll.checked = false;
//break;
}
allCount++;
}
}
if(checkCount != allCount)
{
objChkAll.checked = false;
}
else
{
if(allCount != 0)
{
objChkAll.checked = true;
}
}
}
/**//*
參數說明:
prefix:首碼;chkAll:全選框;chkSingle:單選框ID
type:1【全選】,2【反選】,3【取消】
使用方法:
this.btnSelectAll.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',1);");
this.btnUnSelectAll.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',2);");
this.btnCancelSelect.Attributes.Add("onClick","CheckType('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle',3);");
*/
function CheckType(prefix,chkAll,chkSingle,type)
{
var indexChkAll;
if(prefix.length != 0)
{
indexChkAll = prefix + "__ctl2_" + chkAll;
}
else
{
indexChkAll = chkAll;
}
var objChkAll = do***ent.getElementById(indexChkAll);
var tempObj;
var allCount = 0;
var checkCount = 0;
for(var i=0;i<do***ent.forms[0].elements.length;i++)
{
tempObj = do***ent.forms[0].elements;
if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
{
switch(type)
{
case 1:
tempObj.checked = true;
break;
case 2:
tempObj.checked = !tempObj.checked;
break;
case 3:
tempObj.checked = false;
break;
}
if(tempObj.checked)
{
checkCount++;
}
allCount++;
}
}
if(checkCount != allCount)
{
objChkAll.checked = false;
}
else
{
if(allCount != 0)
{
objChkAll.checked = true;
}
}
window.event.returnValue = false;
return false;
}
/**//*
參數說明:
prefix:首碼;chkAll:全選框;chkSingle:單選框ID
使用方法:
this.btnDelete.Attributes.Add("onClick","SubmitCheckBox('" + this.dg.ClientID.ToString() + "','chkAll','chkSingle');");
*/
function SubmitCheckBox(prefix,chkAll,chkSingle,msg)
{
var indexChkAll;
if(prefix.length != 0)
{
indexChkAll = prefix + "__ctl2_" + chkAll;
}
else
{
indexChkAll = chkAll;
}
var objChkAll = do***ent.getElementById(indexChkAll);
var tempObj;
var allCount = 0;
var checkCount = 0;
for(var i=0;i<do***ent.forms[0].elements.length;i++)
{
tempObj = do***ent.forms[0].elements;
if(tempObj.type == "checkbox" && tempObj.id != indexChkAll && tempObj.id.indexOf(chkSingle) != -1)
{
if(tempObj.checked)
{
checkCount++;
}
allCount++;
}
}
if(allCount == 0) //沒有資料
{
window.alert("當前沒有" + msg + "可供刪除");
window.event.returnValue = false;
return false;
}
else
{
if(checkCount == 0)
{
window.alert("沒有選中要刪除的" + msg + "");
window.event.returnValue = false;
return false;
}
else
{
//if(window.confirm("確定要刪除當前選中的【" + checkCount.toString() + "】項嗎?") == false)
if(window.confirm("確定要刪除當前選中的" + msg + "嗎?") == false)
{
window.event.returnValue = false;
return false;
}
}
}
}