使用Radio按钮选择DataGrid行

 

[日期:2004-10-7]来源:http://www.cnblogs.com/xenogear/  作者:什么都不知道[字体: ]

(读自http://www.dotnetbips.com/displayarticle.aspx?id=147 有源代码下载)
这个问题的出现是因为RadioButtons控件是不能直接加在DataGrid的模板列的。因为DataGrid会给每个单选按钮生成一个唯一名,这样这些单选按钮就不是一个组的了。

解决问题的办法是,在模板列中加入一个Label控件。在DataGrid的ItemDataBound事件中写<INPUT>元素,如:

         If  e.Item.ItemType  =  ListItemType.AlternatingItem  Or  e.Item.ItemType  =  ListItemType.Item  Then
            
Dim  r  As  Label
            r 
=  e.Item.FindControl( " Label2 " )
            r.Text 
=   " <input type=radio name='myradiogroup' value= "   &  e.Item.Cells( 1 ).Text  &   " >"
         End   If


如果要获得选中的值,就要用Request.Form来获取了

         Label3.Text  =  Request.Form( " myradiogroup " )

如果在PostBack之后要保留状态,就要自己做了

         Dim  i  As  DataGridItem
        
For   Each  i  In  DataGrid1.Items
            
If  i.ItemType  =  ListItemType.AlternatingItem  Or  i.ItemType  =  ListItemType.Item  Then
                
Dim  r  As  Label
                r 
=  i.FindControl( " Label2 " )
                
If  r.Text.IndexOf(Label3.Text)  >   0   Then
                    r.Text 
=   " <input type=radio name='myradiogroup' value= "   &  i.Cells( 1 ).Text  &   "  checked>"
                 Else
                    r.Text 
=   " <input type=radio name='myradiogroup' value= "   &  i.Cells( 1 ).Text  &   " >"
                 End   If
            
End   If
        
Next
<script event=onload for=window type=text/javascript>ImgLoad(document.getElementById("BodyLabel"));</script>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值