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

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

1.问题展现

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

?


1

2

3

4

5

6

7

8

9

10

11

12

<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>

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

?


1

2

3

4

5

6

public class Student

  {

    public string SID { get; set; }

    public string SName { get; set; }

    public string SSex { get; set; }

  }

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

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

protected void Page_Load(object sender, EventArgs e)

    {

      if (!IsPostBack)

      {

        this.BindGridView();

      }

    }

    public void BindGridView()

    {

      List<Student> sList = new List<Student>()

      {

        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生成代码如下:

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">

    <tr>

      <th scope="col"> </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属性改一下就好拉!代码如下:

?


1

2

3

4

5

6

7

8

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上面了,囧死了。证据如下:

?


1

2

3

4

5

6

7

8

9

10

<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”,代码如下:

?


1

2

3

4

5

6

7

8

9

10

11

12

<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>

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

?


1

2

3

4

5

6

7

8

9

protected void Page_Load(object sender, EventArgs e)

   {

     if (!IsPostBack)

     {

       this.BindGridView();

       ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),

         "SetRadioName()", true);

     }

   }

问题解决了!

时间: 2024-08-07 21:20:04

ASP.NET GridView中加入RadioButton不能单选的解决方案的相关文章

《转载》ASP.NET GridView中文本内容无法换行(自动换行/正常换行)

ASP.NET GridView中文本内容无法换行(自动换行/正常换行) 作者: 字体:[增加 减小] 类型:转载 用GridView来显示课程表,每个单元格的内容包括课程名.上课地点.教师姓名,然后我想让它们分行显示,感兴趣的朋友可以了解下,或许对你有所帮助 最近做项目的时候,遇到这样一个问题:我用GridView来显示课程表,每个单元格的内容包括课程名.上课地点.教师姓名,然后我想让它们分行显示,效果如下图: 可是呢,GridView太顽强了,不管我拼接字符串时用“\r\n”还是"<b

ASP.NET GridView中文本内容无法换行

第一类:自动换行 GridView默认是自动换行,就是说当显示的字符串比较长的时候,GridView会自动换行. 如果我们不想让它自动换行,在页面后台添加如下代码即可: //正常换行 GridView1.Attributes.Add("style","word-break:keep-all;word-wrap:normal"); 第二类:正常换行 1.应该使用 "<br/>" 2.①如果你绑定字段为设置模版列,那么把对应的BoundF

Gridview中实现RadioButton单选效果

HTML <asp:TemplateField ItemStyle-Width="22px"> <ItemTemplate> <asp:RadioButton ID="radButtonControl" GroupName="group1" runat="server" /> </ItemTemplate> </asp:TemplateField> CS protec

解决radiobutton在gridview中无法单选的一种方法

最近在项目中有个单选gridview中某一项的需求,使用radiobutton后发现,虽然最终选择出来的是一项,但是在页面上却可以选择多项,经过查看生成的html代码,发现生成的radio的name属性并不一样,即使已经加了groupname.解决方法代码如下 页面代码: <asp:GridView ID="SmartGridView1" runat="server" AutoGenerateColumns="False" PageSize

在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView

背景 在前一篇文章<[初学者指南]在ASP.NET MVC 5中创建GridView>中,我们学习了如何在 ASP.NET MVC 中实现 GridView,类似于 ASP.NET web 表单的功能.通过前文,我们已经了解到使用 jQuery 插件的数据表可以很容易地实现具有搜索.排序和分页等重要功能的表格. 前文中需要注意的是,所有通过插件实现的特性都是客户端的,这意味着所有的数据都首先在页面载入,然后由插件来处理客户端搜索.分页和排序的数据.如果数据表不是特别大,这么做是可以的:但是,如

asp.net中父子页面通过gridview中的按钮事件进行回传值的问题

这两天写BS程序,遇到父子页面传值的问题,以前没写过web系统,用了几天时间才将问题解决,总结下记录下来: 问题描述: 父页面A中有一个gridview,每行6个列,有5列中均有一个按钮,单击按钮,会根据列的不同进入不同的子页面,在子页面中有获取数据,并返回父页面的gridview中对应的行的对应列中. 问题关键在于如果确定点击的是gridview的哪一行的哪个按钮,因为数据回传的时候,还要放入该行的该列中. 所以需要考虑一下几个方面: 1.动态添加行,以及各行的各列中的按钮 2.点击某行某列的

GridView 中绑定DropDownList ,下拉框默认选中Label的值

在GridView中,我们 有时候要绑定值. 前台绑定的代码可以这样 <asp:TemplateField HeaderText="当前状态" ItemStyle-HorizontalAlign="Center"> <EditItemTemplate> <asp:DropDownList ID="dStatus" DataSource='<%#BindStatus()%>' DataTextField=&

GridView中的日期处理

数字 {0:N2} 12.36  数字 {0:N0} 13  货币 {0:c2} $12.36  货币 {0:c4} $12.3656  货币  "¥{0:N2}"  ¥12.36  科学计数法 {0:E3} 1.23E+001  百分数 {0:P} 12.25%                         P and p present the same.日期 {0:D} 2006年11月25日  日期 {0:d} 2006-11-25  日期 {0:f} 2006年11月25日

.net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?

http://www.cnblogs.com/summers/archive/2013/07/31/3227234.html .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法,区别? RadioButton实例及说明: <asp:RadioButton ID="publicHas" Checked="true" runat="server" CssClass="radioM