DataGrid实现单选功能,将DataGrid绑定的单选钮放在一个组里

使用DataGrid控件添加单选框时,如果直接在模板中加入RadioButton服务器控件,由于.Net的机制,无法实现单选功能,以下两种方法可以实现:

第一种方法使用Radio标签来实现

 模板列使用html控件Radio标签来绑定

ExpandedBlockStart.gif HTML代码
< asp:TemplateColumn HeaderText = " 是否列表显示 " >
  
< ItemTemplate >
    
< input id = " radIsListShow "  name = " Attribute2 "  type = " radio "  value = ' <%# DataBinder.Eval(Container, "DataItem.SysNo") %> '    /><% # DataBinder.Eval(Container,  " DataItem.Attribute2Name " %>
  
</ ItemTemplate >
</ asp:TemplateColumn >

 

 

后台获取选择的值代码

 

int  iSelectSysNo  =  AppConst.IntNull; // 被选中的属性系统编号
// 判断是否列表显示是否单选
if  (Request.Form[ " Attribute2 " !=   null )
{
    iSelectSysNo 
=   int .Parse(Request.Form[ " Attribute2 " ]);
}
// 处理代码....

 

效果图:

 

 

 

 第二种方法使用RadioButtonList实现

建议使用第一种方法,第二种方法在做样式上处理会比较麻烦,效果图有显示 

ExpandedBlockStart.gif html代码
< asp:TemplateColumn HeaderText = " 是否列表显示 " >
    
< ItemTemplate >
        
< asp:RadioButtonList ID = " radIsListShow "  runat = " server "  Width = " 100% " />
    
</ ItemTemplate >
    
< ItemStyle Width = " 120px "   />
</ asp:TemplateColumn >

 

DataBind之后增加代码

 

ExpandedBlockStart.gif DataBind增加代码
#region  是否列表显示单选列功能

// 将datagrid的第一列设置为模板列,并加入RadioButtonList 
// 将第一列第一单元格的RowSpan设置为DataGrid的总列数 
gridOption.Items[ 0 ].Cells[ 0 ].RowSpan  =  gridOption.Items.Count;
for  ( int  i  =   1 ; i  <  gridOption.Items.Count;  ++ i)
{
    
// 从第二列开始隐藏第一个单元格
    gridOption.Items[i].Cells[ 0 ].Visible  =   false ;  
}

// 将第一列第一个单元格里的RadioButtonList按照DataGrid的总列数进行列添加
for  ( int  i  =   0 ; i  <  gridOption.Items.Count;  ++ i)
{
    Label lblSysNo 
=  gridOption.Items[i].FindControl( " lblSysNo " as  Label;
    Label lblName 
=  gridOption.Items[i].FindControl( " lblName " as  Label;
    ListItem ss 
=   new  ListItem(lblName.Text, lblSysNo.Text);
    ((RadioButtonList)gridOption.Items[
0 ].Cells[ 0 ].Controls[ 1 ]).Items.Add(ss);
}


#endregion

 

获取选中的值代码

 

int  iSelectSysNo  =  AppConst.IntNull; // 被选中的属性系统编号
foreach  (ListItem item  in  radIsListShow.Items)
{
    
if  (item.Selected)
    {
      iSelectSysNo 
=   int .Parse(item.Text);
    }
}

 效果图:当前鼠标是在第一行,第一样颜色有变,但是否列表显示这一列都变色了,因为它都是第一行中的控件。

 

 

转载于:https://www.cnblogs.com/leakey/archive/2011/01/15/1936346.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值