復制代碼 代碼如下:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowIndex = -1;
GridViewRow row = null;
switch (e.CommandName) ...{
case "Command1": // 模板列
// 對于模板列內的按鈕,我們需要顯示綁定行索引到按鈕的 CommandArgument 屬性
// 以獲取觸發事件的行信息
rowIndex = Convert.ToInt32(e.CommandArgument);
row = GridView1.Rows[rowIndex];
DisplayInfo(row, e.CommandName);
// your codes
//
break;
case "Command2": // 模板列
// 同樣處于模板列中,但不采用 Command1 方式,而是通過 NamingContrainer 屬性
// 直接獲取當前的 GridViewRow
Control cmdControl = e.CommandSource as Control; // 表示觸發事件的 IButtonControl,保持統一性并便于后續操作,我們這里直接轉化為控件基類 Control
row = cmdControl.NamingContainer as GridViewRow;
DisplayInfo(row, e.CommandName);
// your codes
//
break;
case "Command3": // 綁定列
// 對于 ButtonField 列,數據源控件內部自動以適當的項索引值填充 CommandArgument 屬性。
// 而無需我們顯示綁定其 CommandArgument 屬性
// 注意,我們這里無法采用 Command2 的方式,對于 BUttonField 觸發的事件,
// GridViewCommandEventArgs.CommandSource 表示的包含此按鈕的 GridView
rowIndex = Convert.ToInt32(e.CommandArgument);
row = GridView1.Rows[rowIndex];
DisplayInfo(row, e.CommandName);
// your codes
//
break;
}
}
新聞熱點
疑難解答
圖片精選