當遇到特殊情況下想自行處理Row事件時,該如何撰寫
或是你想在Row裡放一些button做一些事情,該如何取得Row裡的相關Control資訊
其實在.Net的GridView裡想做單筆儲存或是刪除,己經變的相當容易了
尤其GridView本身也有預設的單筆編輯儲存動作,這部份可以透過一些屬性的設定即可輕鬆達到
所以本篇要寫的部份並不是這個東西
而是當遇到特殊情況下想自行處理Row事件時,該如何撰寫
或是你想在Row裡放一些button做一些事情,該如何取得Row裡的相關Control資訊
假設GridView1在每一筆Row都有二個Button,分別要做不同事情
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Button button = e.CommandSource as Button; //觸發的Button
HiddenField hf1 = button.Parent.Parent.FindControl("HiddenField1") as HiddenField; //Find同一Row裡的Control
HiddenField hf2 = button.Parent.Parent.FindControl("HiddenField2") as HiddenField; //Find同一Row裡的Control
switch (e.CommandName) //利用Button的CommandName分辦是按哪一個Button
{
case "Copy":
//做你想做的處理
break;
case "View":
//做你想做的處理
break;
}
}
{
Button button = e.CommandSource as Button; //觸發的Button
HiddenField hf1 = button.Parent.Parent.FindControl("HiddenField1") as HiddenField; //Find同一Row裡的Control
HiddenField hf2 = button.Parent.Parent.FindControl("HiddenField2") as HiddenField; //Find同一Row裡的Control
switch (e.CommandName) //利用Button的CommandName分辦是按哪一個Button
{
case "Copy":
//做你想做的處理
break;
case "View":
//做你想做的處理
break;
}
}
若本文對您有所幫助,歡迎轉貼,但請在加註【轉貼】及來源出處,並在附上本篇的超連結,感恩您的配合囉。
By No.18