惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
V
V2EX
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
S
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
美团技术团队
S
Security Affairs
AI
AI
SecWiki News
SecWiki News
Hugging Face - Blog
Hugging Face - Blog
B
Blog
小众软件
小众软件
Cloudbric
Cloudbric
V
Visual Studio Blog
PCI Perspectives
PCI Perspectives
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
Schneier on Security
Schneier on Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Vercel News
Vercel News
N
News and Events Feed by Topic
博客园 - 叶小钗
Last Week in AI
Last Week in AI
V
Vulnerabilities – Threatpost
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google Online Security Blog
Google Online Security Blog
TaoSecurity Blog
TaoSecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
SegmentFault 最新的问题
F
Full Disclosure
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
博客园 - Franky

博客园 - xh3

关于cookieless="true"与website.map冲突的一个问题! - xh3 - 博客园 关于AJAX.net的一个问题:回发或回调参数无效! - xh3 - 博客园 在Asp.net 2.0中用QueryString传递中文参数的解决方法! - xh3 - 博客园 在Asp.net 2.0 中禁用页面缓存解决页面刷新(重新加载数据)的问题! 在Asp.net 2.0中关于menu和treeview的几点使用注意! - xh3 - 博客园 Asp.net 2.0在Windows 2003 Server 上配置Microsoft Excel、Microsoft Word应用程序权限时 error: 8000401a 的解决方法! Asp.net 2.0 中 Cache 丢失的问题:“值不能为空”或“未将对象引用设置到对象的实例” 在Asp.net2.0中建立Microsoft.Office.Interop.Word.Application时出现“拒绝访问”错误的解决方法 经典英语口语228句(下) 经典英语口语228句(上) return confirm() 失效的一个解决方法! - xh3 ora-12154 TNS:"无法处理服务名"的一个解决方法 AJAX.NET 1.0 "Sys未定义" 解决方法 - xh3 AJAX.NET 1.0 Configuration Error 解决方法 如何在GridView中通过超级链接列打开服务器上的文档? - xh3 - 博客园 GridViewRow可以任意位置单击引发事件的方法! ASP.NET程序常用代码 - xh3 - 博客园 Windows XP系统优化简单实用版 Windows XP 常见进程列表
如何在GridView中使用RadioButtons单选列!
xh3 · 2006-07-23 · via 博客园 - xh3

在这篇文章中介绍了如何在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"];

试试看,是不是很简单的解决了这个问题!