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

推荐订阅源

宝玉的分享
宝玉的分享
J
Java Code Geeks
S
SegmentFault 最新的问题
L
LangChain Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
IT之家
IT之家
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
H
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Jina AI
Jina AI
N
Netflix TechBlog - Medium
A
About on SuperTechFans
博客园 - 叶小钗
美团技术团队
人人都是产品经理
人人都是产品经理
D
DataBreaches.Net

博客园 - 大牛博客

广东早茶的悠闲时光 用户中心 - 博客园 GridView数据绑定控件和ObjectDataSource数据源控件实现排序功能 - 大牛博客 - 博客园 asp.net 常用字符串过滤方法 非常实用的Asp.net常用的51个代码 - 大牛博客 - 博客园 JS 中面向对象的5钟写法 .netframework3.5 中TimeZoneInfo 类的使用 GridView,DataList,Repeater,DetailsView的遍历行代码 - 大牛博客 - 博客园 ASP.NET 2.0上传图片生成缩略图类 合理选择贪婪模式与非贪婪模式 - 大牛博客 - 博客园 揭开正则表达式的神秘面纱 asp.net下载文件 - 大牛博客 - 博客园 C#数据结构之双向链表 C#数据结构之队列 ASP.NET四种页面导航方式之比较与选择 asp.net页面事件执行顺序 中英文字符串截取方法 - 大牛博客 - 博客园 ASP.NET纯数字验证码 SQL 2005 ROW_NUMBER() 语句分页 | SQL效率最高的分页查询数据
GridView的RowCommand事件中取得行索引 - 大牛博客 - 博客园
大牛博客 · 2009-08-01 · via 博客园 - 大牛博客

前台添加一模版列,里面添加一个Button
<asp:TemplateField HeaderText="测试">
                                  
<ItemTemplate>
                                      
<asp:Button ID="Button1" CommandName="btn" runat="server" Style="position: relative" Text="Button" />
                                  
</ItemTemplate>
                              
</asp:TemplateField>


后台

protected void gv_Company_RowCommand(object sender, GridViewCommandEventArgs e)
    
{
        
if (e.CommandName == "btn")
        
{
            
int index = Convert.ToInt32(e.CommandArgument);
            DataKey key 
= this.gv_Company.DataKeys[index];
            
string tt = key.Value.ToString();

            Response.Write(tt);
        }

    }


    
//行数据绑定
    protected void gv_Company_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.DataRow)
        
{
            Button bt 
= new Button();
            bt 
= (Button)e.Row.Cells[6].FindControl("Button1");
            bt.CommandArgument 
= e.Row.RowIndex.ToString();
        }

    }



ASP.NET2.0中的GRIDVIEW控件真是非常奇怪,不知道MS是怎么考虑的,在GRIDVIEW里,行索引被放在了CommandArgument里面,而不是像DataGrid那样可以利用this.MyDataGrid.DataKeys[e.Item.ItemIndex].ToString()方便的取出主键值,

同时我们注意到,如果使用默认的CommandField

<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />

则可以在RowCommand中使用如下代码取出行号:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

    {

        //编辑按扭

        if (e.CommandName == "Edit")

        {

            //设置编辑行高亮显示

            this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

            //string index= this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            string xh3 = row.Cells[3].Text;

}

}

但问题是,CommandField的可操控性是很底的,我们一般习惯于使用模版列来订制操作按钮,如下:

<asp:TemplateField HeaderText="操作" ShowHeader="False">

                    <EditItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"

                            Text="更新"></asp:LinkButton>

                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"

                            Text="取消"></asp:LinkButton>

                    </EditItemTemplate>

                    <ItemTemplate>

                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"

                            Text="编辑" OnClientClick="return confirm('确认要编辑吗?');"></asp:LinkButton>

                        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Select"

                            Text="选择"></asp:LinkButton>

                        <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"

                            Text="删除" OnClientClick="return confirm('确认要删除吗?');"></asp:LinkButton>

                    </ItemTemplate>

                </asp:TemplateField>


随之而来,问题出现了,运行报错:输入字符串的格式不正确, Convert.ToInt32(e.CommandArgument)中e.CommandArgument转换为字符串为空。当我们把CommandField转换为模版列时,默认的CommandArgument属性丢失了!!!
思考了两天,翻阅了网上的资料,最后在MSDN文档中发现,呈现 GridView 控件之前,必须先为该控件中的每一行创建一个 GridViewRow 对象。在创建 GridView 控件中的每一行时,将引发 RowCreated 事件。这使我们可以提供一个这样的事件处理方法,即每次发生此事件时都执行一个自定义例程(如在行中添加自定义内容,当然也可以添加e.CommandArgument属性为模版列里的LinkButton)。

GridViewRowEventArgs 对象将被传给事件处理方法,随之我们可以访问正在创建的行的属性。若要访问行中的特定单元格,可以使用 GridViewRowEventArgs 对象的 Cells 属性。使用 RowType 属性可确定正在创建的是哪一种行类型(标题行、数据行等等)。

好了,原来我们可以利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。

下面的代码示例演示如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。

 /// <summary>

    /// 为模版列LinkButton写入CommandArgument事件

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            // Retrieve the LinkButton control from the first column.

            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");

            // Set the LinkButton's CommandArgument property with the row's index.

            LinkButton1.CommandArgument = e.Row.RowIndex.ToString();

        }

    }