萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> 在.net中用CheckBoxList實現單選

在.net中用CheckBoxList實現單選

用CheckBoxList實現單選的原因是我覺得CheckBoxList控件頁面展示效果要好看一些,需要的朋友可以參考下 在.net中提供了Radiobuttonlist來實現單選的,但是我一直喜歡用CheckBoxList 原因我覺得CheckBoxList 控件頁面展示效果要好看一些,呵呵    這裡是先CheckBoxList 實現單選采用了控件的點擊事件 調用js來控制單選的    例如頁面如下:   代碼如下: <asp:CheckBoxList ID="CheckBoxList1" BorderWidth="1" runat="server" RepeatLayout="Flow">  <asp:ListItem onclick="CheckBoxList_Click(this)" Value="Item1">Item1</asp:ListItem>  <asp:ListItem onclick="CheckBoxList_Click(this)" Value="Item2">Item2</asp:ListItem>  <asp:ListItem onclick="CheckBoxList_Click(this)" Value="Item3">Item3</asp:ListItem>  <asp:ListItem onclick="CheckBoxList_Click(this)" Value="Item4">Item4</asp:ListItem>  <asp:ListItem onclick="CheckBoxList_Click(this)" Value="Item5">Item5</asp:ListItem>  </asp:CheckBoxList>      這裡是調用的js    原理就是: 1、獲得頁面控件集合,循環查找check    2、設置check 為false ,再將傳入的控件設置選中  代碼如下: function CheckBoxList_Click(sender)  {  var container = sender.parentNode;  if(container.tagName.toUpperCase() == "TD") { // 服務器控件設置呈現為 table 布局(默認設置),否則使用流布局  container = container.parentNode.parentNode; // 層次: <table><tr><td><input />  }  var chkList = container.getElementsByTagName("input");  var senderState = sender.checked;  for(var i=0; i<chkList.length;i++) {  chkList[i].checked = false;  }  sender.checked = senderState;  } 
copyright © 萬盛學電腦網 all rights reserved