日期:2014-05-19  浏览次数:20581 次

请问如何在repeater中进行全选
后台的方法我知道,可惜一选完再按删除的时候会再次触发CheckedChanged事件,最后只会删除第一条记录.
javascript的方法网上有好多,不过我试了几个不是很好用,时间比较紧不能一一再试了.希望高手们帮帮忙,上面两种方法解决一种就可以.如果是javascript请给个简单好用的.

------解决方案--------------------
function fnc2(strid,obj)
{
for(var i=0;i <document.all.length;i++)
{
if(document.all(i).type == 'checkbox ' && document.all(i).parentElement.id == strid) document.all(i).checked = obj.checked;
}
}
------解决方案--------------------
function SelectAll(box)
{

for (var i=0;i <document.form1.elements.length;i++)
{
var e = document.form1.elements[i];
if ( (e.type== 'checkbox ') )
{
var o=e.name.lastIndexOf( 'chkItem ');
if(o!=-1)
{
e.checked = box.checked;

}
}

}
}

<ItemTemplate>
<asp:CheckBox ID= "chkItem " runat= "server " Height= "10 " />
</ItemTemplate>

<input id= "chkHeader " type= "checkbox " title= "Select All " onclick= "SelectAll(this) " /> Select All
------解决方案--------------------
先在repeater中 <ItemTemplate> 加
<asp:CheckBox ID= "Cb_delete " runat= "server "> </asp:CheckBox>
然后利用FindControl找到CheckBox。便可取得是否选中。
CheckBox Cb_delete;
bool Isdelete ;
Label Lb_GradeID;
string gradeID = " ";
deleteState = " ";
foreach (RepeaterItem oR in Rp_GradeList.Items)
{
Cb_delete = (CheckBox)oR.FindControl( "Cb_delete ");
Isdelete = Cb_delete.Checked;
Lb_GradeID = (Label)oR.FindControl( "Lb_GradeID ");
gradeID = Lb_GradeID.Text;
}
同理:全选则在 <HeaderTemplate> 中加一个CheckBox。然后利用类似于上面代码段取得。
------解决方案--------------------
function checkAlls()
{
var checkboxs = document.getElementsByName( "a_name ");
for(i=0;i <checkboxs.length;i++)
{
checkboxs[i].checked = document.getElementById( "checkAll ").checked;
}
}
放服务器端控件就行了!
------解决方案--------------------
以下是用于DataGrid的,Repeater应该是同理
1.JS
//to make all DataGrid CheckBoxs checked
function select_deselectAll(chkVal, idVal) {
var frm = document.forms[0];

// Loop through all elements
for (i=0; i <frm.length; i++) {

// Look for our Header Template 's Checkbox
if (idVal.indexOf ( 'CheckAll ') != -1) {

// Check if main checkbox is checked, then select or deselect datagrid checkboxes
if(chkVal == true)
{
frm.elements[i].checked = true;
}
else
{
frm.elements[i].checked = false;
}
if(frm.elements[i].name.indexOf( 'CancelAll ')!=-1)
{
frm.elements[i].checked = false;
}
  }
 }
}
2.DataGrid
<asp:datagrid id= "dg " runat= "server " Width= "100% " AllowPaging= "True " CellPadding= "3 " AutoGenerateColumns= "False "
PageSize= "20 " ForeColor= "Black " ShowFooter= "False ">