这是我在开发过程中遇到的问题,应该算是比较常见的问题吧。开始以为只要设置一下GroupName就行了,但结果却不行。后面在CSDN上看到了解决办法,先把它记下来。主要方法还是要结合脚本的。
在服务端给GridView添加如下方法:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
rb.Attributes.Add("onclick", "judge(this)"); //给RadioButton添加onclick属性
}
}
在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>
这样就可以了,方法也不复杂。
转载于:https://www.cnblogs.com/maersi/articles/937343.html