限制CheckBoxList控件只能单选
2014-07-09 10:37
441 查看
哈哈,看看Insus.NET做出来的效果:
![](http://images.cnitblog.com/blog/28036/201301/22214351-68c1a153648248a09f87f619a48635b5.gif)
为了你也能实现出来,可以参考下面的方法,第一是准备好一个对象“地支”(Terrestrial Branch)
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <summary> /// Summary description for TerrestrialBranch /// </summary> namespace Insus.NET { public class TerrestrialBranch { private int _ID; private string _Name; public int ID { get{return _ID;} set { _ID = value; } } public string Name { get { return _Name; } set { _Name = value; } } public TerrestrialBranch() { // // TODO: Add constructor logic here // } public TerrestrialBranch(int id, string name) { this.ID = id; this._Name = name; } } }
用数据填充这个对象,并用泛型List<t>来存储这十二个对象:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
private List<TerrestrialBranch> GetData() { List<TerrestrialBranch> tbs = new List<TerrestrialBranch>(); tbs.Add(new TerrestrialBranch(1,"子")); tbs.Add(new TerrestrialBranch(2, "丑")); tbs.Add(new TerrestrialBranch(3, "寅")); tbs.Add(new TerrestrialBranch(4, "卯")); tbs.Add(new TerrestrialBranch(5, "辰")); tbs.Add(new TerrestrialBranch(6, "巳")); tbs.Add(new TerrestrialBranch(7, "午")); tbs.Add(new TerrestrialBranch(8, "未")); tbs.Add(new TerrestrialBranch(9, "申")); tbs.Add(new TerrestrialBranch(10, "酉")); tbs.Add(new TerrestrialBranch(11, "戌")); tbs.Add(new TerrestrialBranch(12, "亥")); return tbs; }
在.aspx页面拉一个CheckBoxList控件,设置两个属性RepeatColumns="6" RepeatDirection="Horizontal"
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
<asp:CheckBoxList ID="CheckBoxListTerrestrialBranch" runat="server" RepeatColumns="6" RepeatDirection="Horizontal"></asp:CheckBoxList>
把刚才准备好的List<TerrestrialBranch>绑定给这个CheckBoxList控件:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Insus.NET; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Data_Binding(); } private void Data_Binding() { this.CheckBoxListTerrestrialBranch.DataSource = GetData(); this.CheckBoxListTerrestrialBranch.DataTextField = "Name"; this.CheckBoxListTerrestrialBranch.DataValueField = "ID"; this.CheckBoxListTerrestrialBranch.DataBind(); } }
OK,一切准备就绪,可以写Javascript脚本,放在<head>之内。
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
window.onload = function () { var cbl = document.getElementById('<%= CheckBoxListTerrestrialBranch.ClientID %>') var inputs = cbl.getElementsByTagName("input"); for (var i = 0; i < inputs.length; i++) { if (inputs[i].type == "checkbox") { inputs[i].onclick = function () { var cbs = inputs; for (var i = 0; i < cbs.length; i++) { if (cbs[i].type == "checkbox" && cbs[i] != this && this.checked) { cbs[i].checked = false; } } } } } }
相关文章推荐
- 限制CheckBoxList控件只能单选实现代码及演示动画
- 限制CheckBoxList控件只能单选
- 一个多选题限制最多只能选N个,CheckBoxList 如何判断
- 限制CheckBox只能单选
- CheckBox(复选按钮)/RadioButton(单选按钮)/BulletedList控件
- 为什么CheckBox只能控制Panel控件的显示,不能控制它的隐藏呢?
- 让CheckBoxList,CheckBox 控件不能操作
- datagrid的几点简单应用,功能包括radio的单选,checkbox的多选,鼠标移动到行上颜色变深,链接传递多参数等功能。这里radio和checkbox都使用的是HTML控件。
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
- ASP.NET DEMO 12 : CheckBoxList 实现单选
- 在服务器端得到CheckBoxList控件最后选择项的方法
- 用JavaScript获取Asp.net服务器端控件CheckBoxList的选中值数组
- ASP.NET2.0中CheckBoxList控件与数据库绑定(学习)
- [导入]在服务器端得到CheckBoxList控件最后选择项的方法
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList
- CheckBoxList控件基本用法
- vs2005入门 之 单选列表(CheckBoxList)和复选列表(RadioButtonList)[视频]
- CheckBoxList控件基本用法 如何给DropDownList控件添加边框[整理]
- ASP.NET 2.0中的友好CSS控件适配器的应用 - 用ul、li呈现CheckBoxList和RadioButtonList