简单实现C#CheckBox单选的相关功能

DataGrid中加入CheckBox,并实现c# CheckBox单选原理就是用js遍列所有CheckBox把所有CheckBox设为未被选中,然后在把单击的CheckBox设为选中。

以下是c# CheckBox单选的实现方法:

aspx文件:

 
 
 
  1. <%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="datagridcheck.webform1" %> 
  2. > 
  3.  
  4.  
  5. </strong>webform1<strong> title></strong>  </li> <li><strong><meta</strong> content="microsoft visual studio .net 7.1" name="generator"<strong>></strong>  </li> <li><strong><meta</strong> content="c#" name="code_language"<strong>></strong>  </li> <li><strong><meta</strong> content="javascript" name="vs_defaultclientscript"<strong>></strong>  </li> <li><strong><meta</strong> content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema"<strong>></strong>  </li> <li><strong><script</strong> language="javascript"<strong>></strong>  </li> <li>function setcheckboxstate()  </li> <li>{  </li> <li>var dom=document.all;  </li> <li>var el=event.srcelement;  </li> <li>if(el.tagname=="input"&&el.type.tolowercase()=="checkbox")  </li> <li>{  </li> <li>for(i=0;i<strong><dom.length</strong>;i++)  </li> <li>{  </li> <li>if(dom[i].tagname=="input"&&dom[i].type.tolowercase()=="checkbox")  </li> <li>{  </li> <li>dom[i].checked=false;  </li> <li>}  </li> <li>}  </li> <li>}  </li> <li>el.checked=!el.checked;  </li> <li>}  </li> <li><strong> script></strong>  </li> <li><strong> head></strong>  </li> <li><strong><body</strong> ms_positioning="gridlayout"<strong>></strong>  </li> <li><strong><form</strong> id="form1" method="post" runat="server"<strong>></strong>  </li> <li><strong><font</strong> face="宋体"<strong>></strong>  </li> <li><strong><asp:datagrid</strong> id="dg" style="z-index: 101; left: 168px; position: absolute; top: 40px" runat="server"  </li> <li>width="440px" autogeneratecolumns="false"<strong>></strong>  </li> <li><strong><columns></strong>  </li> <li><strong><asp:templatecolumn></strong>  </li> <li><strong><itemtemplate></strong>  </li> <li><strong><asp:checkbox</strong> id="chkexport" runat="server"<strong>> asp:checkbox></strong>  </li> <li><strong> itemtemplate></strong>  </li> <li><strong> asp:templatecolumn></strong>  </li> <li><strong><asp:boundcolumn</strong> datafield="integervalue"<strong>> asp:boundcolumn></strong>  </li> <li><strong><asp:boundcolumn</strong> datafield="stringvalue"<strong>> asp:boundcolumn></strong>  </li> <li><strong><asp:boundcolumn</strong> datafield="currencyvalue"<strong>> asp:boundcolumn></strong>  </li> <li><strong> columns></strong>  </li> <li><strong> asp:datagrid><asp:button</strong> id="button1" style="z-index: 102; left: 168px; position: absolute; top: 8px" runat="server"  </li> <li>text="显示内容"<strong>> asp:button> font> form></strong>  </li> <li><strong> body></strong>  </li> <li><strong> html></strong>  </li> </ol></pre><p>以下是cs文件</p><pre> <ol> <li>using system;  </li> <li>using system.collections;  </li> <li>using system.componentmodel;  </li> <li>using system.data;  </li> <li>using system.drawing;  </li> <li>using system.web;  </li> <li>using system.web.sessionstate;  </li> <li>using system.web.ui;  </li> <li>using system.web.ui.webcontrols;  </li> <li>using system.web.ui.htmlcontrols;  </li> <li>namespace datagridcheck  </li> <li>{  </li> <li>/// <strong><summary></strong>  </li> <li>/// webform1 的摘要说明。  </li> <li>/// <strong> summary></strong>  </li> <li>public class webform1 : system.web.ui.page  </li> <li>{  </li> <li>protected system.web.ui.webcontrols.button button1;  </li> <li>protected system.web.ui.webcontrols.datagrid dg;  </li> <li>private void page_load(object sender, system.eventargs e)  </li> <li>{  </li> <li>// 在此处放置用户代码以初始化页面  </li> <li>if (!page.ispostback)  </li> <li>{  </li> <li>dg.datasource= createdatasource() ;  </li> <li>dg.databind();  </li> <li>}  </li> <li>}  </li> <li>icollection createdatasource()  </li> <li>{  </li> <li>datatable dt = new datatable();  </li> <li>datarow dr;  </li> <li>dt.columns.add(new datacolumn("integervalue", typeof(int32)));  </li> <li>dt.columns.add(new datacolumn("stringvalue", typeof(string)));  </li> <li>dt.columns.add(new datacolumn("currencyvalue", typeof(double)));  </li> <li>for (int i = 0; i <strong><</strong> <strong>9</strong>; i++)  </li> <li>{  </li> <li>dr = dt.newrow();  </li> <li>dr[0] = i;  </li> <li>dr[1] = "item " + i.tostring();  </li> <li>dr[2] = 1.23 * (i + 1);  </li> <li>dt.rows.add(dr);  </li> <li>}  </li> <li>dataview dv = new dataview(dt);  </li> <li>return dv;  </li> <li>}  </li> <li>#region web 窗体设计器生成的代码  </li> <li>override protected void oninit(eventargs e)  </li> <li>{  </li> <li>//  </li> <li>// codegen: 该调用是 asp.net web 窗体设计器所必需的。  </li> <li>//  </li> <li>initializecomponent();  </li> <li>base.oninit(e);  </li> <li>}  </li> <li>/// <strong><summary></strong>  </li> <li>/// 设计器支持所需的方法 - 不要使用代码编辑器修改  </li> <li>/// 此方法的内容。  </li> <li>/// <strong> summary></strong>  </li> <li>private void initializecomponent()  </li> <li>{  </li> <li>this.dg.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.dg_itemdatabound);  </li> <li>this.button1.click += new system.eventhandler(this.button1_click);  </li> <li>this.load += new system.eventhandler(this.page_load);  </li> <li>}  </li> <li>#endregion  </li> <li>private void button1_click(object sender, system.eventargs e)  </li> <li>{  </li> <li>system.web.ui.webcontrols.checkbox chkexport;  </li> <li>foreach (datagriditem dgitem in dg.items)  </li> <li>{  </li> <li>chkexport=(checkbox)dgitem.findcontrol("chkexport");  </li> <li>if(chkexport.checked)  </li> <li>{  </li> <li>response.write("<strong><script></strong>alert("+dgitem.cells[2].text+"和"+dgitem.cells[3].text+")<strong> script></strong>");  </li> <li>}  </li> <li>}  </li> <li>}  </li> <li>private void dg_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)  </li> <li>{  </li> <li>if(e.item.itemindex<strong><0</strong>) return;  </li> <li>if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)  </li> <li>{  </li> <li>system.web.ui.webcontrols.checkbox chkexport;  </li> <li>chkexport=(checkbox)e.item.findcontrol("chkexport");  </li> <li>chkexport.attributes.add("onclick","setcheckboxstate()");  </li> <li>e.item.attributes.add("onmouseover","currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=48d1cc");  </li> <li>e.item.attributes.add("onmouseout","this.style.backgroundcolor=currentcolor"); </li> <li>}  </li> <li>}  </li> <li>}  </li> <li>}  </li> </ol></pre><p>以上就是笔者实现C# CheckBox单选事件的方法,希望能给大家带来帮助。</p> <p> 文章名称:<a href="http://www.mswzjz.cn/qtweb/news1/358501.html">简单实现C#CheckBox单选的相关功能</a> <br> 文章源于:<a href="http://www.mswzjz.cn/qtweb/news1/358501.html">http://www.mswzjz.cn/qtweb/news1/358501.html</a> </p> <p> 攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等 </p> <p class="adpic"> <a href="https://www.cdcxhl.com/service/ad.html" target="_blank" class="ad">广告</a> <a href="" target="_blank" class="adimg"><img src=""></a> </p> <p class="copy"> 声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: <a href="http://www.mswzjz.cn/" target="_blank">贝锐智能</a> </p> </div> <div class="newsmorelb"> <p>贝锐智能技术为您推荐以下文章</p> <ul> <li> <a href="/qtweb/news0/358500.html">开展专业的红蓝演练Part.4:论红队的自动化方法</a> </li><li> <a href="/qtweb/news49/358499.html">防止人为误操作MySQL数据库技巧一例</a> </li><li> <a href="/qtweb/news48/358498.html">windows怎么快速打开桌面文件夹选项?(windows设置在哪里打开文件)</a> </li><li> <a href="/qtweb/news47/358497.html">opporeno4安全防火墙怎么改?(游戏服务器防御ddos攻击怎么配置)</a> </li><li> <a href="/qtweb/news46/358496.html">不解密识别恶意流量</a> </li><li> <a href="/qtweb/news45/358495.html">新版本SQLserver带来的革新(SQLserver第二版)</a> </li><li> <a href="/qtweb/news44/358494.html">python发短信_KooPhone可以打电话、发短信吗?</a> </li><li> <a href="/qtweb/news43/358493.html">怎么打开优盘</a> </li><li> <a href="/qtweb/news42/358492.html">国内的免费Java网店系统KgShopV3.0.1云盘版发布!</a> </li> </ul> </div> </div> <div class="col-lg-3 noneb"> <div class="bkright" style="margin-top: 0"> <p><a href="https://www.cdcxhl.com/news/cloud/">云服务器知识</a></p> <ul> <li> <a class="text_overflow" href="/qtweb/news22/264372.html">网速控制怎么设置?(怎么调网速)</a> </li><li> <a class="text_overflow" href="/qtweb/news35/27535.html">UTC时间是什么意思?windows使用utc时间</a> </li><li> <a class="text_overflow" href="/qtweb/news28/119028.html">高防虚拟主机购买怎么测评的</a> </li><li> <a class="text_overflow" href="/qtweb/news16/102666.html">Kotlin函数与函数式编程浅析</a> </li><li> <a class="text_overflow" href="/qtweb/news25/200425.html">vcp认证体系?(vcp服务器)</a> </li><li> <a class="text_overflow" href="/qtweb/news4/249104.html">使用服务器做电脑主机,教你轻松实现!(服务器如何变成电脑主机使用)</a> </li><li> <a class="text_overflow" href="/qtweb/news4/224504.html">Mysql的两行差解密数据表比较操作</a> </li><li> <a class="text_overflow" href="/qtweb/news7/142657.html">纯净版win10系统与专业版win10系统的区别</a> </li><li> <a class="text_overflow" href="/qtweb/news0/477500.html">轻松取名无压力:打造高效的随机取名数据库 (随机取名数据库)</a> </li><li> <a class="text_overflow" href="/qtweb/news26/380226.html">机械表需要电池吗</a> </li><li> <a class="text_overflow" href="/qtweb/news49/526699.html">分布式数据中心的优势</a> </li><li> <a class="text_overflow" href="/qtweb/news10/447660.html">iqooneo5信号不稳定怎么办?(外国网站速度慢怎么办)</a> </li><li> <a class="text_overflow" href="/qtweb/news6/288706.html">Veeam公布2022年勒索软件趋势:受害者只能恢复69%的受损数据</a> </li><li> <a class="text_overflow" href="/qtweb/news38/394538.html">香港服务器带宽型号及其应用领域的全面解析</a> </li><li> <a class="text_overflow" href="/qtweb/news39/373239.html">win10系统如何将我的电脑添加到桌面</a> </li> </ul> </div> <div class="bkright tag"> <p><a href="https://www.cdcxhl.com/hangye/" target="_blank">同城分类信息</a></p> <ul> <li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/snjbc/" target="_blank">水泥搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ruanzhuangsj/" target="_blank">软装设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/led/" target="_blank">LED显示屏</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/fwjd/" target="_blank">房屋鉴定</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/shayanfudiao/" target="_blank">砂岩浮雕</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bxgds/" target="_blank">不锈钢雕塑</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zkj/" target="_blank">公路钻孔机</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zufadianji/" target="_blank">发电机租赁</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/loutihulan/" target="_blank">楼梯护栏</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/ldjbc/" target="_blank">履带搅拌车</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/zzdb/" target="_blank">资质代办</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/chayisj/" target="_blank">茶艺设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/jiulousj/" target="_blank">酒楼设计</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/bdfhw/" target="_blank">被动防护网</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/sdgz/" target="_blank">水电改造</a> </li><li class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> <a href="https://www.cdcxhl.com/hangye/pe/" target="_blank">PE包装袋</a> </li> </ul> </div> </div> </div> <div class="carousel-inner linkbg" style="background: #fff"> <div class="container"> <a href="http://www.nzjierui.cn/" target="_blank">成都发电机组维修保养公司</a>    <a href="http://www.cqcxhl.com/" target="_blank">重庆网站设计</a>    <a href="http://www.cdxwcx.cn/" target="_blank">做网站</a>    <a href="http://www.scarg.cn/" target="_blank">艾瑞格衣柜</a>    <a href="http://www.abwzjs.com/" target="_blank">阿坝网站设计</a>    <a href="http://www.hfwuji.cn/" target="_blank">清舞人间</a>    <a href="http://www.scghjhjc.com/" target="_blank">广汉锦华</a>    <a href="https://www.cdxwcx.com/jifang/ershu.html" target="_blank">成都二枢机房</a>    <a href="https://www.cdxwcx.com/jifang/mianyang.html" target="_blank">绵阳科技城电信IDC机房</a>    <a href="https://www.cdcxhl.com/quanwang.html" target="_blank">全网营销推广</a>    <a href="https://www.cdcxhl.com/yunying.html" target="_blank">成都网站托管</a>    <a href="https://www.cdcxhl.com/city/chengdu.html" target="_blank">四川成都网站建设</a>    <a href="http://chengdu.cdcxhl.cn/shop/ " target="_blank">成都商城网站建设</a>    <a href="http://m.cdcxhl.com/" target="_blank">成都网站制作</a>    <a href="http://www.cdkjz.cn/fangan/door/" target="_blank">门户网站建设方案</a>    <a href="http://www.kswcd.com/" target="_blank">企业网站设计</a>    <a href="http://www.shjike.cn/" target="_blank">宏昇商务服务</a>    <a href="http://www.scghjhjc.cn/" target="_blank">四川锦华橡塑</a>    <a href="http://www.cqjike.cn/" target="_blank">混凝土搅拌罐车</a>    <a href="http://www.clwzsj.com/" target="_blank">成都食品包装设计</a>     </div> </div> <footer> <div class="carousel-inner footjz"> <div class="container"> <i class="icon iconfont zbw"></i> 品质网站制作 <i class="icon iconfont"></i> 自适应网站 <i class="icon iconfont"></i> 开发成本低 <i class="icon iconfont"></i> 响应速度快 <i class="icon iconfont"></i> 多人服务好 <button type="button" class="btn btn-default btn-lg" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 立即开始600网站建设</button> <button type="button" class="btn btn-default btn-xs" onClick="window.location.href='tencent://message/?uin=631063699&Site=&Menu=yes'"> 600网站制作</button> </div> </div> <div class="carousel-inner bqsy"> <div class="container"> <div class="lxfs"> <h4 class="yutelnone">028-86922220 13518219792</h4> <h4 class="yutelblock"><a href="tel:02886922220">028-86922220</a> <a href="tel:13518219792">13518219792</a></h4> <a class="btn btn-default" href="tencent://message/?uin=532337155&Site=&Menu=yes" role="button">网站建设<span>QQ</span>:532337155</a> <a class="btn btn-default" href="tencent://message/?uin=631063699&Site=&Menu=yes" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=532337155&version=1&src_type=web&web_src=oicqzone.com" role="button">网站制作<span>QQ</span>:532337155</a> <a class="btn btn1 btn-default" href="mqqwpa://im/chat?chat_type=wpa&uin=631063699&version=1&src_type=web&web_src=oicqzone.com" role="button">营销推广<span>QQ</span>:631063699</a> <a class="btn btn-default nonea" href="tencent://message/?uin=1683211881&Site=&Menu=yes" role="button">售后QQ:1683211881</a> <div class="dz">创新互联建站专注: <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站设计</a> <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站制作</a> <a href="http://www.mswzjz.cn/" target="_blank">攀枝花网站建设</a> <address>地址:成都太升南路288号锦天国际A幢10楼</address> </div> </div> <div class="bzdh dz"><img src="https://www.cdcxhl.com/imges/bottom_logo.png" alt="创新互联"> <p><a href="https://www.cdcxhl.com/menu.html" target="_blank">成都创新互联科技有限公司</a><br> Tel:028-86922220(7x24h)</p></div> </div> </div> </footer> </body> </html> <script> $.getJSON ("../../qtwebpic.txt", function (data) { var jsonContent = { "featured":data } var random = jsonContent.featured[Math.floor(Math.random() * jsonContent.featured.length)]; $(".adpic .adimg").attr("href",random.link) $(".adpic img").attr("src",random.pic); }) </script>