C# 从页脚模板中的链接按钮获取行索引

C# 从页脚模板中的链接按钮获取行索引,c#,asp.net,gridview,C#,Asp.net,Gridview,我对Gridview的这个问题感到失望 单击页脚行中的链接按钮时,我想在gridview的RowCommand事件中获取行索引 我总是在索引中得到-1作为值 Control ctl = e.CommandSource as Control; GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow; int index = CurrentRow.RowIndex; 如何摆脱这个问题 据

我对Gridview的这个问题感到失望

单击页脚行中的链接按钮时,我想在gridview的RowCommand事件中获取行索引

我总是在
索引中得到-1作为值

        Control ctl = e.CommandSource as Control;
        GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
        int index = CurrentRow.RowIndex;

如何摆脱这个问题

据我所知,页脚行将始终具有RowIndex=-1。我已经尝试过做一些测试,您也可以尝试,对于DataRow类型的每一行,我们都有一个RowIndex,但是对于FooterRow类型的行,我们总是有-1的值。下面是我的测试代码(仅供参考,不需要一些代码行)


我希望这对你有帮助

需要页脚行的索引吗?我想要行号。。因此,是的,您可以为
LinkButton
添加
OnClick
事件,并使用
((GridViewRow)((LinkButton)sender).NamingContainer.RowIndex添加
index
,但我需要gridview的rowcommand事件中的值。然后您可以添加
CommandArgument=“
并通过
e.CommandArgument
查找索引。没有问题。祝你好运
protected void grdGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;
            int index;

            foreach (GridViewRow row in grdGrid.Rows)
            {
                if (CurrentRow.RowType == DataControlRowType.DataRow)
                {
                    //you'll have an RowIndex for each DataRow: 0,1,2 and so on
                    index = CurrentRow.RowIndex;
                }
                else if (CurrentRow.RowType == DataControlRowType.Footer)
                    {
                        //the RowIndex will be always -1 
                        index = CurrentRow.RowIndex;
                    }

            }
        }