在GIIS项目中有一个很帅的需求,先看截图:
[GIIS]CheckBoxList 之 Select All

需求:(下面的功能用JS实现)
1、点击Select All CheckBox, 如果Checked = true, 则下面的CheckBoxList要全部选中,并且Default Value的DropDownList中添加下面CheckBoxList的所有Item, 否则,如果Checked = false, CheckBoxList全部不选,DropDownList清空所有Item。
2、点选CheckBoxList的Item, 如果是选中的,就往DropDownList中添加这个Item并选中,如果是不选中,就从DropDownList删除这个Item, 并且要根据CheckBoxList是否全部选中来决定Select All的选中状态。

解决方法
因为在前台用JS读不到CheckBoxList每个Item的Value和Text, 所以后台给CheckBoxList添加ListValue和ListText两个属性,把所有Item的Value和Text聚合起来,然后在前台读取。每次点击CheckBoxList的一个Item,都循环把所有Item的选中状态按0、1标志,存入一个变量,最后再根据这个标志来决定DropDownList应该添加那些Item。具体请看代码和注释,有更好的解决方法请大家不吝赐教哦。

前台代码:

[GIIS]CheckBoxList 之 Select All<BenQ:QDropDownList ID = "QDropDownListDefaultValue" runat = "server" IsShowDefault = "false" Width = "150px"></BenQ:QDropDownList>
[GIIS]CheckBoxList 之 Select All <input id = "CheckAll" type = "checkbox" onclick = "checkAll(this)" runat = "server" value = "Select All"/> 
[GIIS]CheckBoxList 之 Select All <asp:CheckBoxList  ID="QCheckBoxListAttributeValue" runat="server" RepeatDirection="Horizontal" RepeatLayout ="flow"> </asp:CheckBoxList>

JS代码:

[GIIS]CheckBoxList 之 Select All<script type = "text/javascript">
[GIIS]CheckBoxList 之 Select All 
function checkAll(obj)
[GIIS]CheckBoxList 之 Select All { 
[GIIS]CheckBoxList 之 Select All    
var matchInfo = /^.*?\d+_///用于匹配Repeater中Web控件的客户端ID,比如Repeater1_ct1_
[GIIS]CheckBoxList 之 Select All    var matchObjID =  obj.id.match(matchInfo);    
[GIIS]CheckBoxList 之 Select All    
var halfClientID = matchObjID[0]; 
[GIIS]CheckBoxList 之 Select All    
var checkBoxItemID = halfClientID + "QCheckBoxListAttributeValue";
[GIIS]CheckBoxList 之 Select All    
var dropDownListID  = halfClientID + "QDropDownListDefaultValue";
[GIIS]CheckBoxList 之 Select All    
var checkBoxList = document.getElementsByTagName("input");
[GIIS]CheckBoxList 之 Select All    
var arrListValue = document.getElementById(checkBoxItemID).ListValue.split(','); //得到后台添加的ListValue属性值
[GIIS]CheckBoxList 之 Select All    
var arrListText = document.getElementById(checkBoxItemID).ListText.split(','); //得到后台添加的ListTest属性值
[GIIS]CheckBoxList 之 Select All    
var count = arrListValue.length;
[GIIS]CheckBoxList 之 Select All   document.getElementById(dropDownListID).innerText 
= ""//先清空DropDownList
      //根据Select All的选中状态,循环处理CheckBoxList的每个Item
[GIIS]CheckBoxList 之 Select All    
for (var i=0; i< checkBoxList.length; i++
[GIIS]CheckBoxList 之 Select All    { 
[GIIS]CheckBoxList 之 Select All        
if (checkBoxList[i].id.indexOf(checkBoxItemID) >= 0)
[GIIS]CheckBoxList 之 Select All        { 
[GIIS]CheckBoxList 之 Select All            checkBoxList[i].checked 
= obj.checked;            
[GIIS]CheckBoxList 之 Select All        } 
[GIIS]CheckBoxList 之 Select All   } 
      //根据Select All的选中状态,循环把后台添加的ListValue和ListText属性添加给DropDownList
[GIIS]CheckBoxList 之 Select All    if(obj.checked)
[GIIS]CheckBoxList 之 Select All    {
[GIIS]CheckBoxList 之 Select All         
for(var j = 0; j < count; j++)
[GIIS]CheckBoxList 之 Select All         {
[GIIS]CheckBoxList 之 Select All             
var tOption = document.createElement("Option");
[GIIS]CheckBoxList 之 Select All             tOption.text
=arrListText[j];       
[GIIS]CheckBoxList 之 Select All             tOption.value
=arrListValue[j];
[GIIS]CheckBoxList 之 Select All             document.all(dropDownListID).add(tOption);
[GIIS]CheckBoxList 之 Select All         }
[GIIS]CheckBoxList 之 Select All    }
[GIIS]CheckBoxList 之 Select All    
else
[GIIS]CheckBoxList 之 Select All    {
[GIIS]CheckBoxList 之 Select All        document.getElementById(dropDownListID).innerText 
= ""; //清空DropDownList的所有Item
[GIIS]CheckBoxList 之 Select All   }
[GIIS]CheckBoxList 之 Select All}
[GIIS]CheckBoxList 之 Select All
[GIIS]CheckBoxList 之 Select All 
function checkSingle(checkBoxID, checkValue)
[GIIS]CheckBoxList 之 Select All { 
[GIIS]CheckBoxList 之 Select All    
var matchInfo = /^.*?\d+_/;
[GIIS]CheckBoxList 之 Select All    
var matchObjID =  checkBoxID.id.match(matchInfo);    
[GIIS]CheckBoxList 之 Select All    
var halfClientID = matchObjID[0]; 
[GIIS]CheckBoxList 之 Select All    
var checkBoxItemID = halfClientID + "QCheckBoxListAttributeValue";
[GIIS]CheckBoxList 之 Select All    
var dropDownListID  = halfClientID + "QDropDownListDefaultValue";
[GIIS]CheckBoxList 之 Select All    
var selectAllID =   halfClientID + "CheckAll";
[GIIS]CheckBoxList 之 Select All    
var checkBoxList = document.getElementsByTagName("input");
[GIIS]CheckBoxList 之 Select All    
var arrListValue = document.getElementById(checkBoxItemID).ListValue.split(','); 
[GIIS]CheckBoxList 之 Select All    
var arrListText = document.getElementById(checkBoxItemID).ListText.split(',');
[GIIS]CheckBoxList 之 Select All    
var count = arrListValue.length;
[GIIS]CheckBoxList 之 Select All    
var strCheckChecked = "";  
[GIIS]CheckBoxList 之 Select All    
var arrCheckChecked;
      //每次点击CheckBoxList的一个Item,都循环把所有Item的选中状态按0、1标志,存入一个变量,最后再根据这个标志来决定DropDownList应该添加那些Item
[GIIS]CheckBoxList 之 Select All    
for (var i=0; i< checkBoxList.length; i++
[GIIS]CheckBoxList 之 Select All    { 
[GIIS]CheckBoxList 之 Select All        
if (checkBoxList[i].id.indexOf(checkBoxItemID) >= 0)
[GIIS]CheckBoxList 之 Select All        { 
[GIIS]CheckBoxList 之 Select All            
if(checkBoxList[i].checked)
[GIIS]CheckBoxList 之 Select All            {
[GIIS]CheckBoxList 之 Select All                strCheckChecked 
= strCheckChecked + "1" + ",";
[GIIS]CheckBoxList 之 Select All            }
[GIIS]CheckBoxList 之 Select All            
else
[GIIS]CheckBoxList 之 Select All            {
[GIIS]CheckBoxList 之 Select All                strCheckChecked 
= strCheckChecked + "0" + ",";
[GIIS]CheckBoxList 之 Select All            }
[GIIS]CheckBoxList 之 Select All        } 
[GIIS]CheckBoxList 之 Select All   } 
[GIIS]CheckBoxList 之 Select All    document.getElementById(dropDownListID).innerText 
= "";
[GIIS]CheckBoxList 之 Select All    arrCheckChecked 
=  RTrim(strCheckChecked).split(',');
[GIIS]CheckBoxList 之 Select All     
for(var j = 0; j < count; j++)
[GIIS]CheckBoxList 之 Select All     {
[GIIS]CheckBoxList 之 Select All         
if(arrCheckChecked[j] == "1")
[GIIS]CheckBoxList 之 Select All         {
[GIIS]CheckBoxList 之 Select All             
var tOption = document.createElement("Option");
[GIIS]CheckBoxList 之 Select All             tOption.text
=arrListText[j];           
[GIIS]CheckBoxList 之 Select All             tOption.value
=arrListValue[j];
[GIIS]CheckBoxList 之 Select All             document.all(dropDownListID).add(tOption);
[GIIS]CheckBoxList 之 Select All         }
[GIIS]CheckBoxList 之 Select All     }   
[GIIS]CheckBoxList 之 Select All     
if(strCheckChecked.indexOf("0"<0
[GIIS]CheckBoxList 之 Select All     {
[GIIS]CheckBoxList 之 Select All        document.getElementById(selectAllID).checked 
= true;
[GIIS]CheckBoxList 之 Select All     }
[GIIS]CheckBoxList 之 Select All     
else
[GIIS]CheckBoxList 之 Select All     {
[GIIS]CheckBoxList 之 Select All        document.getElementById(selectAllID).checked 
= false;
[GIIS]CheckBoxList 之 Select All     }
[GIIS]CheckBoxList 之 Select All     
if(checkBoxID.checked) 
[GIIS]CheckBoxList 之 Select All    {
[GIIS]CheckBoxList 之 Select All        document.all(dropDownListID).value 
=checkValue; 
[GIIS]CheckBoxList 之 Select All    }
[GIIS]CheckBoxList 之 Select All}
[GIIS]CheckBoxList 之 Select All
[GIIS]CheckBoxList 之 Select All
function RTrim(str) 
[GIIS]CheckBoxList 之 Select All
[GIIS]CheckBoxList 之 Select All    
var i; 
[GIIS]CheckBoxList 之 Select All    
for(i=str.length-1;i>=0;i--
[GIIS]CheckBoxList 之 Select All    { 
[GIIS]CheckBoxList 之 Select All        
if(str.charAt(i)!=","&&str.charAt(i)!=",")break
[GIIS]CheckBoxList 之 Select All    } 
[GIIS]CheckBoxList 之 Select All    str
=str.substring(0,i+1); 
[GIIS]CheckBoxList 之 Select All    
return str; 
[GIIS]CheckBoxList 之 Select All[GIIS]CheckBoxList 之 Select All
[GIIS]CheckBoxList 之 Select All
</script>

后台代码:

[GIIS]CheckBoxList 之 Select AllCheckBoxList CheckBoxListValue = e.Item.FindControl("QCheckBoxListAttributeValue"as CheckBoxList;
[GIIS]CheckBoxList 之 Select All
DropDownList dropDownListDefaultValue = e.Item.FindControl("QDropDownListDefaultValue"as DropDownList;
[GIIS]CheckBoxList 之 Select All
HtmlInputCheckBox checkAll = e.Item.FindControl("CheckAll"as HtmlInputCheckBox;
[GIIS]CheckBoxList 之 Select AllCheckBoxListValue.DataSource = plValue;
[GIIS]CheckBoxList 之 Select All                CheckBoxListValue.DataTextField 
= "attribute_value_name";
[GIIS]CheckBoxList 之 Select All                CheckBoxListValue.DataValueField 
= "attribute_value_id";
[GIIS]CheckBoxList 之 Select All                CheckBoxListValue.DataBind();
[GIIS]CheckBoxList 之 Select All                
//get checkboxlist's value and text for js control ---add by sammy song 2007-08-07
[GIIS]CheckBoxList 之 Select All
                string checkListValue = "";
[GIIS]CheckBoxList 之 Select All                
string checkListText = "";
[GIIS]CheckBoxList 之 Select All                
for (int i = 0; i < plValue.Count; i++)
);
                  //因为在前台用JS读不到CheckBoxList每个Item的Value和Text, 所以后台添加ListValue和ListText两个属性,把所有Item的Value和Text聚合起来,然后在前台读取
[GIIS]CheckBoxList 之 Select All                CheckBoxListValue.Attributes[
"ListValue"= checkListValue;
[GIIS]CheckBoxList 之 Select All                CheckBoxListValue.Attributes[
"ListText"=
 checkListText;
[GIIS]CheckBoxList 之 Select All                
//end add
[GIIS]CheckBoxList 之 Select AlldropDownListDefaultValue.Items.Clear();
[GIIS]CheckBoxList 之 Select All            
bool allSelected = true;
[GIIS]CheckBoxList 之 Select All            
foreach (ListItem item in CheckBoxListValue.Items)
            }

相关文章:

  • 2021-05-31
  • 2021-09-07
  • 2022-12-23
  • 2021-07-25
  • 2021-05-23
  • 2021-08-22
  • 2022-01-14
  • 2021-11-16
猜你喜欢
  • 2021-07-29
  • 2022-12-23
  • 2022-01-09
  • 2021-06-21
  • 2021-08-27
  • 2021-07-19
相关资源
相似解决方案