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