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

2025-05-29 0 62

今天开发碰见一个问题,就是当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();

}

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

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

什么原因呢?细心的人可能会说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);

}

}

问题解决了!

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.NET GridView中加入RadioButton不能单选的解决方案 https://www.kuaiidc.com/100891.html

相关文章

发表评论
暂无评论