• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ASP.NET GridView中加入RadioButton不能单选的解决方案

     今天开发碰见一个问题,就是当GridView中加入一个包含RadioButton的模板列,结果一运行。。。。。天啊,单选按钮可以多选了! 囧啊!为了演示一下我今天的错误我还是模拟一个功能场景吧,我要实现的功能是显示一个包含单选按钮的学生信息列表,选择一行后将详细信息显示出来~!

    1.问题展现

    ①首先准备一个GridView用来展示学生的基本信息与最重要的单选按钮,代码如下:

     asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        Columns>
        asp:TemplateField>
         ItemTemplate>
         asp:RadioButton ID="rbStudent" runat="server" />
         /ItemTemplate>
        /asp:TemplateField>
    
        asp:BoundField DataField="SName" HeaderText="用户名" />
        asp:BoundField DataField="SSex" HeaderText="性别" />
        /Columns>
        /asp:GridView>
    

    ②接下来准备需要绑定数据的实体,代码如下: 

    public class Student
      {
        public string SID { get; set; }
        public string SName { get; set; }
        public string SSex { get; set; }
      }
    

    ③初始化数据,绑定GridView列表,代码如下:

    protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
          {
            this.BindGridView();
          }
        }
    
        public void BindGridView()
        {
          ListStudent> sList = new ListStudent>()
          {
            new Student(){ SID = "s001", SName="张三", SSex="男"},
            new Student(){ SID = "s002", SName="李四", SSex="女"},
            new Student(){ SID = "s003", SName="王五", SSex="男"}
          };
    
          GridView1.DataSource = sList;
          GridView1.DataBind();
        }
    
    

    这个时候看起来没有什么问题,但是一运行我确发现,单选框失去了本身的作用,如图:

            

    什么原因呢?细心的人可能会说RadioButton你没有设置GroupName属性所以不属于一组,但事实我设置了该属性后还是无效!

    2.解决思路

    ① 问题分析

    在运行后,我右键查看源代码后发现了这个诡异的问题,原来是GridView会自动给input 的name属性赋值,导致了每一行的name属性都不一样,造成了不能单选的问题,GridView生成代码如下:

    table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
        tr>
          th scope="col">nbsp;/th>th scope="col">用户名/th>th scope="col">性别/th>
        /tr>tr>
          td>
         input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" />
         /td>td>张三/td>td>男/td>
        /tr>tr>
          td>
         input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" />
         /td>td>李四/td>td>女/td>
        /tr>tr>
          td>
         input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" />
         /td>td>王五/td>td>男/td>
        /tr>
      /table>

    可以发现每一个RadioButton控件的name属性都不一样,导致了不能单选的问题,那么如何解决掉这个罪魁祸首呢?

    我第一个想到的办法就是人工将name属性改为统一的,那么如何改呢?有的人可能会说那很简单啊,使用GridView的OnRowDataBound事件在行绑定的时候讲RadioButton的name属性改一下就好拉!代码如下: 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          if (e.Row.RowType == DataControlRowType.DataRow)
          {
            RadioButton RadioButtionRB = (RadioButton)e.Row.FindControl("rbStudent");
            RadioButtionRB.Attributes["name"] = "student";
          }
        }

    但是运行后,我又失望了,什么原因呢? 是因为RadioButton在客户端输出的时候外面会有一层SPAN>标记,name属性被加到SPAN上面了,囧死了。证据如下:

    span name="student">input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" />/span>
         /td>td>张三/td>td>男/td>
        /tr>tr>
          td>
         span name="student">input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" />/span>
         /td>td>李四/td>td>女/td>
        /tr>tr>
          td>
         span name="student">input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" />/span>
         /td>td>王五/td>td>男/td>

    看来这种思路行不通啊,只能用JS啦,所以正题来了,我决定在数据绑定后通过JS遍历GridView中的RadioButton将name属性改为相同的值,行得通吗?试试看呗!         
    ② 问题解决
    首先先来实现一个JS函数,用来获取遍历GridView中的RadioButton并将其name属性设置为一个统一的值,例如“myradio”,代码如下:

     script type="text/javascript">
        function SetRadioName() {
          var gv = document.getElementById("GridView1"); //获取GridView的客户端ID
          var myradio = gv.getElementsByTagName("input"); //获取GridView的Inputhtml
          for (var i = 0; i  myradio.length; i++) {
            if (myradio[i].type == 'radio')//hidden
            {
              myradio[i].setAttribute("name", "myradio");
            }
          }
        }
      /script>
    

    接下来在绑定数据后注册调用这段脚本,或者将该脚本写到页面标签视图的最下面,代码如下:

     protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
          {
            this.BindGridView();
            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
              "SetRadioName()", true);
          }
        }

    问题解决了!

    经过一番努力问题终于得到完美解决,整个问题分析思路清晰,希望可以真正帮助到亲们解决类似问题,有好的想法,欢迎大家一起探讨

    您可能感兴趣的文章:
    • Android利用GridView实现单选功能
    • asp.net GridView中使用RadioButton单选按钮的方法
    • gridview中实现radiobutton的单选示例
    • DataGridView中CheckBox实现某一列单选
    • Gridview使用CheckBox全选与单选采用js实现同时高亮显示选择行
    • js实现GridView单选效果自动设置交替行、选中行、鼠标移动行背景色
    • asp.net 扩展GridView 增加单选按钮列的代码
    • Android利用GridView实现单选效果
    上一篇:如何解决ASP.NET新增时多字段取值的问题
    下一篇:阿里云上从ASP.NET线程角度对“黑色30秒”问题的全新分析
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    ASP.NET GridView中加入RadioButton不能单选的解决方案 ASP.NET,GridView,中,加入,RadioButton,