现在的位置: 首页 > 综合 > 正文

[DataGrid技巧]将RadioButton加入到DataGrid模板列后单选简单实现

2011年07月04日 ⁄ 综合 ⁄ 共 985字 ⁄ 字号 评论关闭

SourceCode:TestSingleSelect.rar

先写下Javascript:

<script language="javascript">

  var flag =0;   
  
var lastObj;   
   
 
function CancelSelect(obj){   
   
  
if (flag == 0)
  {   
    lastObj 
= obj;   
    flag
+=1;   
  }
  
else
  {   
    
if (lastObj.id != obj.id){   
         lastObj.checked 
= false;   
        lastObj 
= obj;   
     }  
     
  }
 }   
</script>

在模板列中添加Radiobutton,并且激发javascript:

  <ItemTemplate>
   
<asp:RadioButton id="rbSelect"  onclick="javascript:CancelSelect(this);" runat="server"></asp:RadioButton>
  
</ItemTemplate>

在后置代码中得到选择的RadioButton

        Dim DemoGridItem As DataGridItem
        
For Each DemoGridItem In grdEmployees.Items
            
Dim myRadioButton As RadioButton = CType(DemoGridItem.Cells(0).Controls(1), RadioButton)
            
If myRadioButton.Checked = True Then
                myRadioButton.Checked 
= False
                
'Get Select RadioButton
            End If
        
Next

当然还有一些不错的方法,我把一篇文章转载我的Bolg上.RadioButton加入DataGrid模板列引起的问题

CodeProject:How to group RadioButtons

抱歉!评论已关闭.