如何取得Gridview內自訂按鍵所屬的RowIndex

如何取得Gridview內自訂按鍵所屬的RowIndex

取得Button物件後再找NamingContainer的RowIndex

方法1:在on_click 時取得

 protected void Button1_Click(object sender, EventArgs e)
    {
        int rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
    }

方法2:在RowCommand取得

   protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {

         //設定按鍵的RowCommandName 是GridInsert,判斷是Button傳來的RowCommand
         if (e.CommandName == "GridInsert")          {
            GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int rowindex = rowSelect.RowIndex;
        }

參考網址:

http://stackoverflow.com/questions/8526427/get-selected-index-in-gridview-by-clicking-on-button

http://stackoverflow.com/questions/8699772/row-index-of-linkbutton-in-gridview