GridView中实现单选RadioButton

[img]http://img319.ph.126.net/9jQjsMOmmoQeyOB9pGnVnQ==/3826933783358209061.jpg[/img]
[color=red]注意:若在gridview内对radiobutton用组名groupname同名方法行不同,若加上GroupName="skytest"查看静态页面源代码便知道:两个name值的组名都不同。[/color]


[img]http://img542.ph.126.net/jhJXv7zY94mMXlnkw2k5Bg==/1329687789982224141.jpg[/img]

下面结合js实现单选.

呈现页:

<asp:GridView ID="GridView1" Width="960px" EmptyDataText="暂无标书可操作"
DataKeyNames="ID" AutoGenerateColumns="false" runat="server" onrowdatabound="GridView1_RowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
[color=red]<asp:RadioButton ID="RadioButton1" runat="server" />[/color]
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="标书编号" />

</Columns>
</asp:GridView>

代码页:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
if (rb != null)
{
[color=red]rb.Attributes.Add("onclick", "single(this)"); //single(obj)为js函数[/color]
}
}

}

需要添加的javascript函数:

<script type="text/javascript">
var last=null;
function single(obj)
{
if(last==null) //第一次选择RadioButton时赋id值给last
{
last=obj.id;
}
else //第一次以后的每一次都在这运行,把上此的RadioButton.Checked=false,记下此次的obj.name
{
var lo=document.getElementById(last);
lo.checked=false;
last=obj.id;
}
obj.checked="checked"; //添加checked属性,以便在上边赋值为false
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值