如何在GridView中使用RadioButtons单选列,并且获取所选定RadioButton的值。

我们习惯性的做法是在GridView的TemplateField使用RadioButton server control;正如我们在GridView的TemplateField使用checkboxes来完成多选功能一样,我们可以通过遍历行来获取选定checkboxes的值,但RadioButtons的区别之处在于同一页面状态之间只能选取一个RadioButton。那么我们可能会想如何赋予每一个RadioButton不同的名称以取得各自的值,但不幸的是GridView在生成行的同时没有为每一个RadioButton赋予不同名称。该如何解决呢?
我们可以利用HTML RadioButton来巧妙的解决这个问题! 仅仅只需要在GridView的TemplateField使用HTML RadioButton来代替RadioButton server control。

<ASP:GridView ID="GridView1" runat="server"          AutoGenerateColumns="False" BackColor="White"         BorderColor="#CC9966" BorderStyle="None"          BorderWidth="1px" CellPadding="4" Font-Names="Verdana">   <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

  <Columns>

      <ASP:BoundField DataField="CategoryID" HeaderText="CategoryID" />       

<ASP:BoundField DataField="CategoryName" HeaderText="CategoryName" />     

  <ASP:BoundField DataField="Description" HeaderText="Description" />    

   <ASP:TemplateField HeaderText="Select One">         <ItemTemplate>           <input name="MyRadioButton" type="radio"                      value='<%# Eval("CategoryID") %>' />         </ItemTemplate>       </ASP:TemplateField>   </Columns>   <RowStyle BackColor="White" ForeColor="#330099" />   <SelectedRowStyle BackColor="#FFCC66"            Font-Bold="True" ForeColor="#663399" />   <PagerStyle BackColor="#FFFFCC"            ForeColor="#330099" HorizontalAlign="Center" />   <HeaderStyle BackColor="#990000"            Font-Bold="True" ForeColor="#FFFFCC" /> </ASP:GridView> 在该TemplateField中我们设置了radio,并将其值绑定为我们需要获取的行记录的主键! 当radio button被选定时,我们通过表单对象获取被选定的radio button的值。 string selectedValue = Request.Form["MyRadioButton"]; 试试看,是不是很简单的解决了这个问题!

 

转载于:https://www.cnblogs.com/www56/p/7073857.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值