嵌套的在GridView里的radiobutton 怎么才能只选中一个

GridView1  中有一列从数据库加载数据  ,并且加一列模板列,在编辑模板中  给该列加上radiobutton1  
但是运行起来后   那列所有radiobutton可被选中请问怎样处理才能让只选中一个啊!!!
这是今天在CSDN上看见有个朋友问的问题,我看见有的人说指定同一个GroupName属性,但是这个不管用,有人说用HTML控件代替服务器端控件,我觉得HTML空间不灵活,服务器端根据选择结果处理的时候不太好用,还有人说用JavaScript遍历整个GridView,我觉得这样也不好。我后来想了一个觉得跟前面几个比都要好一点的办法,用服务器端控件但是有不去遍历GridView的方法:
在服务器端给 GridView添加如下方法
1 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 if (e.Row.RowType == DataControlRowType.DataRow)
4 {
5 RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
6 rb.Attributes.Add("onclick", "judge(this)");//给RadioButton添加onclick属性
7 }
8 }

在aspx页面里添加下面的JS脚本
<script type="text/javascript">
var last = null;//最后访问的RadioButton的ID
function judge(obj)
{
if(last == null)
{
last
= obj.id;
//alert(last);
}
else
{
var lo = document.getElementById(last);
lo.checked
= "";
//alert(last + " " + lo.checked);
last = obj.name;
}
obj.checked
= "checked";
}
</script>

这样就行了!呵呵,我觉得的至少比遍历GridView要好吧!
原文地址:https://www.cnblogs.com/405464904/p/1533932.html