在DataGrid或DataList等容器上面实现RadioButton的单选功能

今天遇到个问题 在DataGrid里做一个RadionButton控件想实现单选的功能,可是却无法进行RadioButton的单选, 原因是因为DataGrid的模版列会自动为每个空间起个新名字, 而在DataGrid显示后, 这些RadioButton的名字是不相同的, 包括其GroupName也不同, 而客户端的<input type="radio">恰恰是通过name属性来分组的, 这样就没有办法在DataGrid中实现RadioButton的单选, 而是每个都可以选, 变成CheckBox了。


网上查了资料 具体解决办法如下 (转自http://blog.csdn.net/mib23/archive/2006/04/14/663740.aspx

下面已DataGrid为例子,在模板列上加一个RadioButton:radio1
<ItemTemplate>
   <asp:RadioButton id="radio1"  οnclick="javascript:CancelSelect(this,'myDataGrid');" runat="server"></asp:RadioButton>
  </ItemTemplate>

myDataGrid即为DataGrid的名称,目的是为了如果页面上还有别的RadioButton,可以互不影响,单选myDataGrid中的RadioButton只对本容器中的RadioButton有影响。

Javascript脚本为

<script language="javascript">
 function CancelSelect(obj,tempSpan)
 {
 elem=obj.form.elements; 
 var strTemp = tempSpan;
 for(i=0;i<elem.length;i++)
 {
  if (elem[i].type=="radio" && elem[i].id != obj.id && obj.name.substr(0,elem[i].name.indexOf(':')) == strTemp)
  {
   elem[i].checked = false;
  }
 }
 }  
</script>


以上自己测试过 成功!
还是希望能帮助到有同样需要的朋友!


转载于:https://www.cnblogs.com/Monday/archive/2006/11/29/576950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值