iT邦幫忙

0

詢問一個 ASP.NET C# 問題

Label LB = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1")

抱歉問一個菜逼八的問題
Button1_Click上無法使用Rows[e.RowIndex]
這裡要改成什麼,才能知道使用者在GridView內,按下哪個"選取" 按鈕?

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
glj8989332
iT邦研究生 4 級 ‧ 2020-07-28 16:26:29
最佳解答

參考這個 How to get row data by clicking a button in a row in an ASP.NET gridview

你的gridview欄位應該是這樣設定?

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" 
                    OnClick="Button1_Click" />
    </ItemTemplate>
</asp:TemplateField>

然後按鍵事件的處理:

protected void Button1_Click(object sender, System.EventArgs e)
{
    Button btn = (Button)sender;
    
    // 取得按下按鈕的那一列
    GridViewRow gvr = (GridViewRow)btn.NamingContainer;
} 
看更多先前的回應...收起先前的回應...
阿偉 iT邦新手 2 級 ‧ 2020-07-28 17:41:38 檢舉

你好,我GridView是轉成樣板沒錯
我語法是這樣

protected void Button1_Click(object sender, EventArgs e)
{
	Label LB = (Label)GridView1.Rows[0].FindControl("Label1");
	string s_url;
	s_url = "B.aspx?name=" + LB.Text;
	Response.Redirect(s_url);
}

使用你的語法,gvr 沒辦法帶入Rows[]內

還是你的語法要用別的方式傳值?

我要使用者選取後,取得Label1的值,在把Label1值傳出去
我原本的方式是可以傳值出去,但沒辦法判別使用者在GridView內是選哪個按鈕

glj8989332 iT邦研究生 4 級 ‧ 2020-07-28 22:50:54 檢舉

既然有了 GridViewRow....那就可以直接這樣用呀

Label LB = (Label) gvr.FindControl("Label1");
tx50xyz iT邦新手 4 級 ‧ 2020-07-29 09:03:55 檢舉

應因有二種,一種是為了抓使用者所選的欄位,指定的ID號碼(cs裡寫的),另一種是抓的是觸發的物件(ASPX裡寫的),這二種都可以取到你要的值,只是用法不同,視情況而寫。

阿偉 iT邦新手 2 級 ‧ 2020-07-29 09:22:33 檢舉

感謝您~可以用了!!

glj8989332 iT邦研究生 4 級 ‧ 2020-07-29 09:26:16 檢舉

恭喜阿

我要發表回答

立即登入回答