ASP.NET GridView中加入RadioButton不能单选的解决方案
2021-07-15 02:06
标签:group 今天 pen box center dex syntax mil ora 今天开发碰见一个问题,就是当GridView中加入一个包含RadioButton的模板列,结果一运行。。。。。天啊,单选按钮可以多选了! 囧啊!为了演示一下我今天的错误我还是模拟一个功能场景吧,我要实现的功能是显示一个包含单选按钮的学生信息列表,选择一行后将详细信息显示出来~! 1.问题展现 ①首先准备一个GridView用来展示学生的基本信息与最重要的单选按钮,代码如下: ②接下来准备需要绑定数据的实体,代码如下: ③初始化数据,绑定GridView列表,代码如下: 这个时候看起来没有什么问题,但是一运行我确发现,单选框失去了本身的作用,如图: 什么原因呢?细心的人可能会说RadioButton你没有设置GroupName属性所以不属于一组,但事实我设置了该属性后还是无效! 2.解决思路 ① 问题分析 在运行后,我右键查看源代码后发现了这个诡异的问题,原来是GridView会自动给input 的name属性赋值,导致了每一行的name属性都不一样,造成了不能单选的问题,GridView生成代码如下: 可以发现每一个RadioButton控件的name属性都不一样,导致了不能单选的问题,那么如何解决掉这个罪魁祸首呢? 我第一个想到的办法就是人工将name属性改为统一的,那么如何改呢?有的人可能会说那很简单啊,使用GridView的OnRowDataBound事件在行绑定的时候讲RadioButton的name属性改一下就好拉!代码如下: 但是运行后,我又失望了,什么原因呢? 是因为RadioButton在客户端输出的时候外面会有一层标记,name属性被加到SPAN上面了,囧死了。证据如下: 看来这种思路行不通啊,只能用JS啦,所以正题来了,我决定在数据绑定后通过JS遍历GridView中的RadioButton将name属性改为相同的值,行得通吗?试试看呗! ② 问题解决 首先先来实现一个JS函数,用来获取遍历GridView中的RadioButton并将其name属性设置为一个统一的值,例如“myradio”,代码如下: 接下来在绑定数据后注册调用这段脚本,或者将该脚本写到页面标签视图的最下面,代码如下: 问题解决了! ASP.NET GridView中加入RadioButton不能单选的解决方案 标签:group 今天 pen box center dex syntax mil ora 原文地址:http://www.cnblogs.com/www56/p/7073857.html
"GridView1" runat="server" AutoGenerateColumns="false">
"rbStudent" runat="server" />
"SName" HeaderText="用户名" /> "SSex" HeaderText="性别" />
public class Student
{
public string SID { get; set; }
public string SName { get; set; }
public string SSex { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
}
}
public void BindGridView()
{
Listnew List
{
new Student(){ SID = "s001", SName="张三", SSex="男"},
new Student(){ SID = "s002", SName="李四", SSex="女"},
new Student(){ SID = "s003", SName="王五", SSex="男"}
};
GridView1.DataSource = sList;
GridView1.DataBind();
}

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>
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";
}
}
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>
>
function SetRadioName() {
var gv = document.getElementById("GridView1"); //获取GridView的客户端ID
var myradio = gv.getElementsByTagName("input"); //获取GridView的Inputhtml
for (var i = 0; i
if (myradio[i].type == ‘radio‘)//hidden
{
myradio[i].setAttribute("name", "myradio");
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
"SetRadioName()", true);
}
}
文章标题:ASP.NET GridView中加入RadioButton不能单选的解决方案
文章链接:http://soscw.com/index.php/essay/105374.html