閱讀749 返回首頁    go 阿裏雲 go 技術社區[雲棲]


GridView的RowCommand事件中取得行索引 技巧

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow)//如果是為數據行 { ImageButton imgbtnup = (ImageButton)e.Row.Cells[1].FindControl("btnMoveUp");//找控件 imgbtnup.CommandArgument = e.Row.RowIndex.ToString();//設置與此BUTTON關聯的命令參數 imgbtnup.Visible = e.Row.RowIndex != 0; ImageButton imgbtndown = (ImageButton)e.Row.Cells[2].FindControl("btnMoveDown"); imgbtndown.CommandArgument = e.Row.RowIndex.ToString(); imgbtndown.Visible = e.Row.RowIndex != ((DataSet)((GridView)sender).DataSource).Tables[0].Rows.Count - 1; } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "MoveUp") { int index = Convert.ToInt32(e.CommandArgument);//取的行索引 DataKey key = this.GridView1.DataKeys[index]; string keyval = key.Value;//取得主鍵 } else if (e.CommandName == "MoveDown") { int index = Convert.ToInt32(e.CommandArgument); DataKey key = this.GridView1.DataKeys[index]; string keyval = key.Value; } }

最後更新:2017-04-02 06:51:35

  上一篇:go ubuntu下安裝顯卡驅動
  下一篇:go 堆棧解析算術表達式