也许各位看官还尚未注意到,将RadioButton加入到DataGrid模板列后(当然DataList,Repeater也一样),尽管你设置了其GroupName,结果还是不能实现我们想要的效果, 即实现RadioButton之初衷:实现单选效果。
也许,你会说,用RadioButtonList即可解决,如:

 1 <asp:datagrid id="DataGrid1" runat="server" 
 2 //1、我们将datagrid的第一列设置为模板列,并加入RadioButtonList 
 3 AutoGenerateColumns="False">
 4 <Columns>                      <asp:TemplateColumn>
 5     <ItemTemplate>                    <asp:RadioButtonList ID="RadioButtonList1" Runat="server"></asp:RadioButtonList>
 6      </ItemTemplate>
 7   </asp:TemplateColumn>                <asp:BoundColumn DataField="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
 8 </Columns>
 9 </asp:datagrid>
10 //然后在数据绑定到DataGrid后,即DataGrid1.DataBind();代码之后,写:
11 //将第一列第一单元格的RowSpan设置为DataGrid的总列数
12 DataGrid.Items[0].Cells[0].RowSpan=DataGrid.Items.Count;  
13 for (int i=1;i<DataGrid.Items.Count;++i)
14 {              DataGrid.Items[i].Cells[0].Visible=false;  
15 //从第二列开始隐藏第一个单元格
16 }
17 //将第一列第一个单元格里的RadioButtonList按照DataGrid的总列数进行列添加
18 for (int i=0;i<DataGrid.Items.Count;++i)
19 {
20 ListItem li=new ListItem("","1");
21 ((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).Items.Add(li);
22 }
23 而确定哪项被选中,可通过 DataGrid1.DataKeys[((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).SelectedIndex] 
24 得到。

当然,最简单的方法就是使用<input type="radio" name=“radioDemo">HTML控件,使用简单,就不再重复了。
而如果我们想用RadioButton实现呢?
这时候,我们就必须思考为什么
<input type="radio" name="radioDemo">可以实现单选

<input type="radio" name="radioDemo" id="radioDemo" runat="server">
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Demo" />
却不能实现单选呢?
如果我们查看浏览器输出的源代码,会发现
Runat="server"的RadioButton的Name变成了类似

1 <input id="countriesGrid__ctl2_selectRadioButton"  type="radio" name="countriesGrid:_ctl2:country" value="selectRadioButton" />

这样的Name了,故其实现不了单选。
所以,我们只有让DataGrid输出唯一的ClientID
以下为实现代码:

  1 using System;
  2 using System.Web.UI;
  3 using System.Web.UI.WebControls;
  4 using System.Globalization;
  5 
  6 namespace Renyu.Web.UI.WebControls
  7 {
  8     [ToolboxData("<{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")]
  9     public class GroupRadioButton : RadioButton, IPostBackDataHandler
 10     {
 11         public GroupRadioButton() : base()
 12         {
 13         }
 14 
 15         #region Properties
 16 
 17         private string Value
 18         {
 19             get
 20             {
 21                 string val = Attributes["value"];
 22                 if(val == null)
 23                     val = UniqueID;
 24                 else
 25                     val = UniqueID + "_" + val;
 26                 return val;
 27             }
 28         }
 29 
 30         #endregion
 31         
 32         #region Rendering
 33 
 34         protected override void Render(HtmlTextWriter output)
 35         {
 36             RenderInputTag(output);
 37         }
 38 
 39         private void RenderInputTag(HtmlTextWriter htw)
 40         {
 41             htw.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
 42             htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
 43             htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
 44             htw.AddAttribute(HtmlTextWriterAttribute.Value, Value);
 45             if(Checked)
 46                 htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
 47             if(!Enabled)
 48                 htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
 49             
 50             string onClick = Attributes["onclick"];
 51             if(AutoPostBack)
 52             {
 53                 if(onClick != null)
 54                     onClick = String.Empty;
 55                 onClick += Page.GetPostBackClientEvent(this, String.Empty);
 56                 htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
 57                 htw.AddAttribute("language", "javascript");
 58             }
 59             else
 60             {
 61                 if(onClick != null)
 62                     htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
 63             }
 64 
 65             if(AccessKey.Length > 0)
 66                 htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
 67             if(TabIndex != 0)
 68                 htw.AddAttribute(HtmlTextWriterAttribute.Tabindex, 
 69                     TabIndex.ToString(NumberFormatInfo.InvariantInfo));
 70             htw.RenderBeginTag(HtmlTextWriterTag.Input);
 71             htw.RenderEndTag();
 72         }
 73 
 74         #endregion
 75 
 76         #region IPostBackDataHandler Members
 77 
 78         void IPostBackDataHandler.RaisePostDataChangedEvent()
 79         {
 80             OnCheckedChanged(EventArgs.Empty);
 81         }
 82 
 83         bool IPostBackDataHandler.LoadPostData(string postDataKey, 
 84             System.Collections.Specialized.NameValueCollection postCollection)
 85         {
 86             bool result = false;
 87             string value = postCollection[GroupName];
 88             if((value != null) && (value == Value))
 89             {
 90                 if(!Checked)
 91                 {
 92                     Checked = true;
 93                     result = true;
 94                 }
 95             }
 96             else
 97             {
 98                 if(Checked)
 99                     Checked = false;
100             }
101             return result;
102         }
103 
104         #endregion
105     }
106 }
107 

编译为.dll后,即可像System.Web.UI.WebControls.RadioButton一样使用了。

RadioButton加入DataGrid模板列引起的问题。相关推荐

  1. ASP.NET DEMO 14: 如何在 GridView/DataGrid 模板列中使用自动回发的 CheckBox/DropDownList

    有时候希望在 GridView 模板中使用自动回发的 CheckBox (autopostback=true) ,但是 CheckBox 没有 CommandName 属性,因此也就无法在 GridV ...

  2. ASP.NET基础教程-DataGrid表格控件-模板列的使用

    一.给表格添加一个模板列,在查板列中添加一个文本框,文本框名字为"txt" 1.对文本框进行赋值: ((TextBox)DataGrid.Items[0].Cells[0].Fin ...

  3. 动态创建模板列并绑定数据(GridView,Repeater,DataGrid)

    要创建动态模板,请创建以后需要时可实例化的模板类. 创建模板类 创建实现 System.Web.UI.ITemplate 接口的新类. 您也可以将值传递到类的构造函数,类可以使用该值来确定要创建的模板 ...

  4. DataGrid动态绑定模板列

    需求说明:在DataGrid中显示手机短信过滤类型(手机号码,业务推广短信,帐户变动短信,节日祝福短信),表示是否向手机号码相应类型的短信.由于被过滤的短信类型可能会动态添加,因此绑定DataGrid ...

  5. 怎样让WinForms下DataGrid可以像ASP.NET下的DataGrid一样使用自定义的模板列

    昨天被问到一个问题:怎么把WinForms里的DataGrid的绑定了数据库bit字段的列默认显示的CheckBox换成"男"和"女",也就是说怎么样像ASP. ...

  6. Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果

    Silverlight使用DataGrid的模板列(DataGridTemplateColumn)实现类似TreeListView控件的效果 转载于:https://www.cnblogs.com/K ...

  7. 动态模板列更新数据分页的例子

    前台: <%@ Page language="c#" Codebehind="WebForm30.aspx.cs" AutoEventWireup=&qu ...

  8. 寻找GridView中模板列中的控件

    假如你在gridview中添加一个模板列,并 在模板列中存放了一个dropdownlist控件.那么,问题就是:你如何去操作这个dropdownlist控件???? //对于gridview控件:  ...

  9. 反射实体自动生成EasyUi DataGrid模板 第二版--附项目源码

    之前写过一篇文章,地址  http://www.cnblogs.com/Bond/p/3469798.html   大概说了下怎么通过反射来自动生成对应EasyUi datagrid的模板,然后贴了很 ...

最新文章

  1. 关闭虚拟机提示“正在处理另一个任务”解决方法
  2. 诺奖经济学家:中国与世界可找到差异化空间推动合作共赢
  3. c++ map 析构函数_C++|类继承关系中的虚函数、虚析构函数、虚基类
  4. Qt6.2.1使用clang格式化代码
  5. Java语言所有异常类均继承自_要继承自定义异常类的继承方式必须使用 ( ) 关键字_学小易找答案...
  6. Android开发笔记(四十三)点击事件
  7. c# 溢出抛异常_C#中的int是否没有溢出异常?
  8. sqlite3简单使用
  9. 翻译:Vim从入门到精通 Mac OS
  10. 5G消息RCS富媒体通信与传统短信相比有何特色?
  11. 决策树系列(二)——基于决策树算法实现泰坦尼克号生还预测
  12. 数据仓库应用篇(一)需求文档模板和需求评审
  13. Learning without Forgetting 详解(LwF)
  14. 这算通过审核了么?接下来还有没有什么坑,求大神指点!--酷课堂iOS交流群问答精华整理(201808期)
  15. 参禅静坐--虚极静笃--快速恢复脑力体力
  16. MinIO The access key ID you provided does not exist in our records
  17. 西藏春运送服务 让旅客带着温暖出发
  18. 清华论文CH-SIMS: A Chinese Multimodal Sentiment Analysis Dataset with Fine-grained Annotations of Modali
  19. PWM信号通过功率三极管控制电机,PWM波形失真问题。
  20. AP微积分到底选AB还是BC?

热门文章

  1. PHp批量推送数据太慢,PHP非阻塞批量推送数据-php教程
  2. jdk中的动态代理和cglib中动态代理的区别
  3. 【自动驾驶】25.激光雷达 标定 相机
  4. CNN 中1X1卷积核的作用
  5. OpenCV学习笔记大集锦
  6. Programming Computer Vision with Python (学习笔记八)
  7. AbstractQueuedSynchronizer 原理分析 - Condition 实现原理
  8. 全面探索 FreeMarker 模版引擎的扩展性
  9. Java程序员从笨鸟到菜鸟之(一百零八)一步一步学习webservice(二)webservice基本原理
  10. Apriori算法简介及实现(python)