当字段中使用OnCommand参数时,如何在GridView中获取rowindex

时间:2011-04-21 05:53:22

标签: c# asp.net gridview

<asp:LinkButton ID="lblViewDetail" runat="server" style="text-decoration:underline;"  OnCommand="ViewSummary" CommandArgument='<%#Eval("ShowlinkDetail") %>' Text='View Detail' />

我在.cs文件中有一个方法,在数据字段中有这个方法名称调用

OnCommand="ViewSummary"
private void ViewSummary()
{
     //code
}

当我点击标签控件上的网格视图时,我想获得Rowindex.Please帮助。

3 个答案:

答案 0 :(得分:2)

您的linkbutton的 OnCommand 将是这样的

protected void lnk_Command(object sender, CommandEventArgs e)
{
   // The Namingcontainer would be Gridrow
   // You can get the rowindex in this fashion
    ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex    
}

答案 1 :(得分:1)

它会返回当前行。你不需要排索...

if(e.CommandName == "")
{
GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
}

答案 2 :(得分:0)

LinkBut​​ton

CommandArgument='<%#((GridViewRow)Container).RowIndex%>'
.cs中的

protected void gv_CommDetails_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            try
            {
                int index = Convert.ToInt32(e.CommandArgument); //Here you can get the index.

                if (e.CommandName == "ViewSummary")
                {
                   drp_Comm.Items.FindByValue(((HiddenField)gv_CommDetails.Rows[index].Cells[0].FindControl("hdn_CommCode")).Value).Selected = true;
                   txt_CommDescription.Text =gv_CommDetails.Rows[index].Cells[2].Text;
                }

            }

            catch (Exception ee)
            {
                string message = ee.Message;
            }

        }