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

推荐订阅源

P
Proofpoint News Feed
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
Martin Fowler
Martin Fowler
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
The Cloudflare Blog
T
Tailwind CSS Blog
H
Help Net Security
腾讯CDC
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
C
Check Point Blog
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - 芸

Xtragrid的图片显示 N×N矩阵螺旋打印输出 数组int[],int[,]的区别 转载 使用ActiveReport for .net 进行报表开发 oracle数据库之间的数据拷贝 VS2003 水晶报表服务器端部署 SQL操作全集 合并多行的字符串—oarcle(转) 在网页上使用户按Enter键自动跳到下一控件,并禁止使用鼠标右键和其他快捷键的HTC控件 字符串样式的格式化 oracle 存储过程的基本语法 2007年终总结 主从表XtarGrid设计 JavaScript面向对象 dotNet常用快捷键 最美的十大经典爱情句子 自定义控件脚本校验 ActiveReport子报表 正则表达式收藏
在DataGrid中使用单选框
· 2007-01-22 · via 博客园 - 芸

   在DataGrid中使用单选框

其实现步骤如下

1.              DataGrid中添加一列模板列,在模板列中加入单选框,但是不能用


RadioButton,这样就不能达到效果,其HTML代码为:

<asp:datagrid id="DataGrid1" runat="server" Width="100%" AutoGenerateColumns ="False">
    
<AlternatingItemStyle Height="22px" BackColor="#F8F8F8"></AlternatingItemStyle>
    
<ItemStyle HorizontalAlign="Center" Height="22px" BackColor="White"></ItemStyle>
<HeaderStyle HorizontalAlign="Center" Height="25px" CssClass="lab" BackColor = "#B4D6EE"></HeaderStyle>
    
<Columns>
        
<asp:TemplateColumn HeaderText="选择">
            
<HeaderStyle Width="5%"></HeaderStyle>
            
<ItemTemplate>
                
<input type=radio name="RadioName" 
value
= '<%# DataBinder.Eval(Container.DataItem, "Serial")%>'/>
            
</ItemTemplate>
        
</asp:TemplateColumn>
    
</Columns>
</asp:datagrid>


2.数据源绑定

单选框的Value值应该设置为可以唯一标识该记录的值,比如主键、序号。这边举例绑定的是序号,其字段为:Serial。再获取数据后,手动添加序号列,其代码如下:

if(dtSource != null && dtSource.Rows.Count > 0)
{
    dtSource.Columns.Add(
"Serial",Type.GetType("System.String"));
    
for(int i = 0;i<dtSource.Rows.Count;i++)
    
{
        dtSource.Rows[i][
"Serial"= i.ToString();
    }

}

//绑定数据
DataGrid1.DataSource = dtSource;
DataGrid1.DataBind();

3.获取选中记录

   该函数返回选中记录的序号,如果返回值是-1表示没有选中的记录。

private int GetSerial()
        
{
            
if(Request.Form["RadioName"!= null)
            
{
                
try
                

                
catch
                
{
                    
return -1;
                }

            }

            
else
            
{
                
return -1;
            }

}