2017-04-07 68 views
0

簡單的問題,我的搜索似乎是空白。使GridView AutGenerateColumn發生RowBmand觸發RowCommand事件

我有一個GridView即AutoGeneratingColumns=True

這產生一列 「MyColumn」。我想讓這個列的值是一個可點擊的LinkBut​​ton,它使用特定的CommandNmae觸發GridView的RowCommand事件,並將單元格值作爲CommandArgument。

我想要做到這一點,而不使用自定義的ItemTemplate,我知道該怎麼做。我希望我可以實際修改GridView中自動生成的列,如上所述。

蒂亞

回答

0

這可以從OnRowDataBound事件GridView控件來完成。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the current row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     ///create a new linkbutton 
     LinkButton button = new LinkButton(); 
     button.Text = "LinkButton"; 
     button.CommandArgument = "MyArgs"; 

     //assign a command to the button 
     button.Command += Button_Command; 

     //add the button to cell x 
     e.Row.Cells[3].Controls.Add(button); 
    } 
} 

因爲你動態地添加控制,在GridView的結合應該不爲IsPostBack檢查裏面包裹着。你通常會做的事。

+0

我如何處理這個如果我的GridView是綁定在一個按鈕甚至點擊(在前一個屏幕上)? – Nugs

+0

您必須在某處「存儲」點擊,就像在會話中一樣。因此,當頁面重新加載時,您可以執行與按鈕單擊相同的代碼。 – VDWWD