温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# 中怎么利用CheckBox实现单选功能

发布时间:2021-07-07 17:45:06 来源:亿速云 阅读:392 作者:Leah 栏目:编程语言

今天就跟大家聊聊有关C# 中怎么利用CheckBox实现单选功能,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

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

aspx文件:

<%@ page language="c#" codebehind="webform1.aspx.cs" autoeventwireup="false" inherits="datagridcheck.webform1" %>   <!doctype html public "-//w3c//dtd html 4.0 transitional//en" >   <html>   <head>   <title>webform1title>   <meta content="microsoft visual studio .net 7.1" name="generator">   <meta content="c#" name="code_language">   <meta content="javascript" name="vs_defaultclientscript">   <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema">   <script language="javascript">   function setcheckboxstate()   {   var dom=document.all;   var el=event.srcelement;   if(el.tagname=="input"&&el.type.tolowercase()=="checkbox")   {   for(i=0;i<dom.length;i++)   {   if(dom[i].tagname=="input"&&dom[i].type.tolowercase()=="checkbox")   {   dom[i].checked=false;   }   }   }   el.checked=!el.checked;   }   script>   head>   <body ms_positioning="gridlayout">   <form id="form1" method="post" runat="server">   <font face="宋体">   <asp:datagrid id="dg" style="z-index: 101; left: 168px; position: absolute; top: 40px" runat="server"   width="440px" autogeneratecolumns="false">   <columns>   <asp:templatecolumn>   <itemtemplate>   <asp:checkbox id="chkexport" runat="server">asp:checkbox>   itemtemplate>   asp:templatecolumn>   <asp:boundcolumn datafield="integervalue">asp:boundcolumn>   <asp:boundcolumn datafield="stringvalue">asp:boundcolumn>   <asp:boundcolumn datafield="currencyvalue">asp:boundcolumn>   columns>   asp:datagrid><asp:button id="button1" style="z-index: 102; left: 168px; position: absolute; top: 8px" runat="server"   text="显示内容">asp:button>font>form>   body>   html>

以下是cs文件

using system;   using system.collections;   using system.componentmodel;   using system.data;   using system.drawing;   using system.web;   using system.web.sessionstate;   using system.web.ui;   using system.web.ui.webcontrols;   using system.web.ui.htmlcontrols;   namespace datagridcheck   {   /// <summary>   /// webform1 的摘要说明。   /// summary>   public class webform1 : system.web.ui.page   {   protected system.web.ui.webcontrols.button button1;   protected system.web.ui.webcontrols.datagrid dg;   private void page_load(object sender, system.eventargs e)   {   // 在此处放置用户代码以初始化页面   if (!page.ispostback)   {   dg.datasource= createdatasource() ;   dg.databind();   }   }   icollection createdatasource()   {   datatable dt = new datatable();   datarow dr;   dt.columns.add(new datacolumn("integervalue", typeof(int32)));   dt.columns.add(new datacolumn("stringvalue", typeof(string)));   dt.columns.add(new datacolumn("currencyvalue", typeof(double)));   for (int i = 0; i < 9; i++)   {   dr = dt.newrow();   dr[0] = i;   dr[1] = "item " + i.tostring();   dr[2] = 1.23 * (i + 1);   dt.rows.add(dr);   }   dataview dv = new dataview(dt);   return dv;   }   #region web 窗体设计器生成的代码   override protected void oninit(eventargs e)   {   //   // codegen: 该调用是 asp.net web 窗体设计器所必需的。   //   initializecomponent();   base.oninit(e);   }   /// <summary>   /// 设计器支持所需的方法 - 不要使用代码编辑器修改   /// 此方法的内容。   /// summary>   private void initializecomponent()   {   this.dg.itemdatabound += new system.web.ui.webcontrols.datagriditemeventhandler(this.dg_itemdatabound);   this.button1.click += new system.eventhandler(this.button1_click);   this.load += new system.eventhandler(this.page_load);   }   #endregion   private void button1_click(object sender, system.eventargs e)   {   system.web.ui.webcontrols.checkbox chkexport;   foreach (datagriditem dgitem in dg.items)   {   chkexport=(checkbox)dgitem.findcontrol("chkexport");   if(chkexport.checked)   {   response.write("<script>alert("+dgitem.cells[2].text+"和"+dgitem.cells[3].text+")script>");   }   }   }   private void dg_itemdatabound(object sender, system.web.ui.webcontrols.datagriditemeventargs e)   {   if(e.item.itemindex<0) return;   if(e.item.itemtype == listitemtype.item || e.item.itemtype == listitemtype.alternatingitem)   {   system.web.ui.webcontrols.checkbox chkexport;   chkexport=(checkbox)e.item.findcontrol("chkexport");   chkexport.attributes.add("onclick","setcheckboxstate()");   e.item.attributes.add("onmouseover","currentcolor=this.style.backgroundcolor;this.style.backgroundcolor=48d1cc");   e.item.attributes.add("onmouseout","this.style.backgroundcolor=currentcolor");  }   }   }   }

看完上述内容,你们对C# 中怎么利用CheckBox实现单选功能有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI