ASP.NET DEMO 12 : CheckBoxList 实现单选
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET DEMO 12 : CheckBoxList 实现单选
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一看標(biāo)題估計(jì)大家都開始懷疑了:單選?為什么不直接使用 RadioButtonList ?
是的。你是對(duì)的。然而,實(shí)際應(yīng)用中需求千變?nèi)f化,誰(shuí)讓我們的客戶夠 BT 呢?
主要代碼
只有一個(gè)通用的 CheckBoxList_Click 函數(shù),
需要注意的是 CheckBoxList 可以呈現(xiàn)為 table 布局,也可以呈現(xiàn)為流布局(使用 span 做外部容器)
我的習(xí)慣是,腳本代碼中,盡量不直接引用 html id,因?yàn)閷?duì)于服務(wù)器控件對(duì)應(yīng)的是 ClientID,而ClientID與控件層次關(guān)聯(lián)的,不利于代碼移植復(fù)用,因此盡可能選擇直接傳遞對(duì)象,通過(guò) DOM 獲取相關(guān)的父控件和子控件。
????{
????????var?container?=?sender.parentNode;????????
????????if(container.tagName.toUpperCase()?==?"TD")?{?//?服務(wù)器控件設(shè)置呈現(xiàn)為?table?布局(默認(rèn)設(shè)置),否則使用流布局
????????????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;??????????
????}
<h3>單選效果的?CheckBoxList</h3>
????<div?style="float:left">
????<h4>靜態(tài)項(xiàng)</h4>
????????<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>
????</div>
????<div?style="float:left;padding-left:100px">
????<h4>綁定項(xiàng)</h4>
????????<asp:CheckBoxList?ID="CheckBoxList2"?BorderWidth="1"?runat="server"?DataTextField="Value"?DataValueField="Key"?OnDataBound="CheckBoxList2_DataBound">????????
????????</asp:CheckBoxList>
????</div>
兼容性
IE 6 SP6, ?FF 2.0, ?Opera 9.2 測(cè)試通過(guò)
頁(yè)面效果
下載
轉(zhuǎn)載于:https://www.cnblogs.com/Jinglecat/archive/2007/07/18/823201.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET DEMO 12 : CheckBoxList 实现单选的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 苍天啊,请你不要再哭泣
- 下一篇: Linq 入门系列 [OfType,To