Re: [問題] GridView按鈕按下後無法產生RowCommand
看板C_Sharp (C#)作者TeemingVoid (TeemingVoid)時間13年前 (2012/01/20 00:30)推噓2(2推 0噓 9→)留言11則, 5人參與討論串3/3 (看更多)
※ 引述《epola (Epola)》之銘言:
: 請問各位高手,
: 最近我新學GridView遇到一個問題,
: 當在GridView中加入Button時 (commandname沒輸入)
: 由設計頁面去點選Button時,並不會主動生成
: void xxxGridView_RowCommand(Object sender, GridViewCommandEventArgs e){略}
: 因為我需要取得目前取得的button屬於哪一個row
: 卻都會產生
: void xxxGridView_SelectedIndexChanged(Object sender, EventArgs e)
不論是在 GridView 裏頭的哪一個按鈕,也不論是在 GridView 的哪一個部分,只要
是在 GridView 上頭點兩下,產生的預設事件程序就是 SelectedIndexChanged 事件。
: 而若是自行加上
: <asp:GirdView ID="GirdView1" runat="server" OnRowCommand="xxxGridView_RowCommand">
: </asp:GirdView>
: 還是會出現錯誤訊息。
: 不知道是否有哪個步驟出現錯誤 ?
建議您這麼做試試看:
1. 先點一下設計畫面的 GridView
2. 再點一下右下角的「屬性」視窗的「事件」按鈕(圖面像一道閃電)。
(按下功能鍵 F4 可顯示「屬性」視窗)
3. 在事件表中,點兩下 RowCommand 的輸入方塊,這樣就可產生出事件處理程序。
另外,你的按鈕的 CommandName 最好還是設一下比較好,因為,切換頁次、
排序、編輯、更新、取消等等這些 GridView 的 LinkButton 也都會觸發
RowCommand 事件,可透過 CommandName 分辨是否是你的按鈕 Clicked。
最後,如何找到「哪一個Row」的程式寫法如下,一併提供給您參考,希望會有幫助: :)
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "btnXXX")
{
// iRow 即是該列的編號
int iRow = Convert.ToInt32(e.CommandArgument);
// string sData = GridView1.Rows[iRow].Cells[1].Text
// ...
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.41.100.238
→
01/20 00:34, , 1F
01/20 00:34, 1F
→
01/20 00:36, , 2F
01/20 00:36, 2F
→
01/20 00:39, , 3F
01/20 00:39, 3F
→
01/20 00:40, , 4F
01/20 00:40, 4F
推
01/20 00:47, , 5F
01/20 00:47, 5F
→
01/20 00:48, , 6F
01/20 00:48, 6F
→
01/20 00:50, , 7F
01/20 00:50, 7F
→
01/20 00:50, , 8F
01/20 00:50, 8F
推
01/20 10:33, , 9F
01/20 10:33, 9F
→
01/23 23:57, , 10F
01/23 23:57, 10F
→
01/24 00:34, , 11F
01/24 00:34, 11F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章