.NET 控制多项选择的选择数目、多选取值

Calchas 发表于 2009, February 9, 3:13 PM

JS

 

JavaScript代码
  1. function GoCheck(CheckBoxID,ItemCount,MaxItem)    
  2. {    
  3. var iCount = 0;    
  4. for(var i=0;i<ItemCount;i++)    
  5. {    
  6. var chk = document.getElementById(CheckBoxID+"_"+i);    
  7. if(chk != null )    
  8. {    
  9. if(chk.checked)    
  10. iCount ++;    
  11. }    
  12. }    
  13. if(iCount > MaxItem)    
  14. {    
  15. alert("最多只能选择"+MaxItem+"项");    
  16. return false;    
  17. }   
  18. }  

 

HTML

 

XML/HTML代码
  1. <tr class="title"><td><a id="A4">(4)影响您选择留学国家的因素主要是(请选择两项):</a></td></tr>  
  2.         <tr><td><asp:CheckBoxList ID="RadioButtonList4" runat="server" RepeatColumns="3" RepeatDirection="Horizontal">  
  3.         <asp:ListItem Text="A、资金" Value="A"></asp:ListItem>  
  4.         <asp:ListItem Text="B、语言" Value="B"></asp:ListItem>  
  5.         <asp:ListItem Text="C、学校和专业" Value="C"></asp:ListItem>  
  6.         <asp:ListItem Text="D、国家政治经济环境" Value="D"></asp:ListItem>  
  7.         <asp:ListItem Text="E、移民政策" Value="E"></asp:ListItem>  
  8.         <asp:ListItem Text="F、其他(请填写)" Value="F"></asp:ListItem>  
  9.         </asp:CheckBoxList>  
  10.         <asp:TextBox ID="txtRadioButtonList4" runat="server" CssClass="otheritem"></asp:TextBox><br /></td></tr>  

 

C S

 

C#代码
  1. //设定点击事件   
  2. this.RadioButtonList4.Attributes.Add("onclick""javacript:return GoCheck('RadioButtonList4'," + this.RadioButtonList4.Items.Count + ",2);");   
  3.   
  4. //多选取值   
  5. string Question4 = CheckBoxValue(RadioButtonList4).ToString();   
  6.   
  7.   
  8.     protected string CheckBoxValue(CheckBoxList cbl)   
  9.     {   
  10.         string s = "";   
  11.         for (int i = 0; i < cbl.Items.Count; i++)   
  12.         {   
  13.             ListItem li = cbl.Items[i];   
  14.             if (li.Selected)   
  15.             {   
  16.                 if (s == "")   
  17.                 {   
  18.                     s += li.Value;   
  19.                 }   
  20.                 else  
  21.                 {   
  22.                     s += "," + li.Value;   
  23.                 }   
  24.             }   
  25.         }   
  26.         return s;   
  27.     }  

« 上一篇 | 下一篇 »

标签: asp.net
相关文章: (最多只显示5条记)
引用: 点击获得Trackback地址,Encode: UTF-8 点击获得Trackback地址,Encode: GB2312 or GBK 点击获得Trackback地址,Encode: BIG5
发表评论:( 你的参与是我最大的动力! )