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

推荐订阅源

D
DataBreaches.Net
F
Fortinet All Blogs
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
罗磊的独立博客
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
U
Unit 42
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Proofpoint News Feed
G
Google Developers Blog
H
Help Net Security

博客园 - LeeXiaoLiang

自己写的一个javascript下拉列表类 jquery1.5.1根据元素ID获取元素对象 jquery向.ashx文件post中文乱码问题的解决 【转】GridView的RowCommand事件中取得行索引 - LeeXiaoLiang - 博客园 【转】GridView 实现服务器端和客户端全选的两种方法 ASP.NET之GridView数据绑定 - LeeXiaoLiang - 博客园 希尔排序的C语言实现(2) 希尔排序的C语言实现(1) 简单插入排序的C语言实现 堆排序的C语言实现 对于堆排序算法的理解 【转】sqlserver中分页方法集锦 【转】高效的MySQL分页 【摘】完全二叉树 【原创】连接字符串数组 【摘】用C实现将数组转换为字符串 【原创】用C实现Trim()函数 - LeeXiaoLiang - 博客园 【转】浅谈数据库设计技巧 [转]数据库设计范式的理解
【转】通过在RowDataBound事件中把行索引绑定到控件的Command...
LeeXiaoLiang · 2010-12-07 · via 博客园 - LeeXiaoLiang

代码

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    
int rowIndex = -1;
    GridViewRow row 
= null;        
    
switch (e.CommandName)
    {
        
case "Command1"// 模板列
        
// 对于模板列内的按钮,我们需要显示绑定行索引到按钮的 CommandArgument 属性
        
// 以获取触发事件的行信息
         rowIndex = Convert.ToInt32(e.CommandArgument);
         row 
= GridView1.Rows[rowIndex];                
         DisplayInfo(row, e.CommandName);
        
// your codes
        
// 
        break;
        
case "Command2"// 模板列
        
// 同样处于模板列中,但不采用 Command1 方式,而是通过 NamingContrainer 属性
        
// 直接获取当前的 GridViewRow
         Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
         row = cmdControl.NamingContainer as GridViewRow;
         DisplayInfo(row, e.CommandName);
        
// your codes
        
// 
        break;
        
case "Command3"// 绑定列
        
// 对于 ButtonField 列,数据源控件内部自动以适当的项索引值填充 CommandArgument 属性。
        
// 而无需我们显示绑定其 CommandArgument 属性                
        
// 注意,我们这里无法采用 Command2 的方式,对于 BUttonField 触发的事件,
        
// GridViewCommandEventArgs.CommandSource 表示的包含此按钮的 GridView
         rowIndex = Convert.ToInt32(e.CommandArgument);
         row 
= GridView1.Rows[rowIndex];
         DisplayInfo(row, e.CommandName);
        
// your codes
        
// 
        break;
     }
}