2013年7月3日 星期三

ASP.NET 點選GridView中的Button,並取得該列(Row)的欄位值

如題,就是當我點選GridView中的Button時,我同時也要取得被我點選該列的欄位值。這樣的小技巧常會被用來更新資料或者當我按下按鈕時,切換畫面後並將GridView中的資料順便傳給下一個畫面...等技巧就看您怎麼去運用。這個問題去搜尋都找得到,自此我只是將經驗記錄下來。

前置作業:
1.準備好資料庫(待會要讓GridView顯示資料用)
以上準備好後,就跟在步驟做一遍吧~

步驟如下:
Step 1.紅色的部份請在GridView中加上
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:TemplateField HeaderText="Button">
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" Text="Show 1" 
                           onclick="Button1_Click" />
                        <asp:Button ID="Button2" runat="server" Text="Show 2"  
                           CommandName="ShowData" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="test_id" HeaderText="test_id" InsertVisible="False"
                    ReadOnly="True" SortExpression="test_id" />
                <asp:BoundField DataField="test_name" HeaderText="test_name"
                    SortExpression="test_name" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [test_T]"></asp:SqlDataSource>

Step 2.因我將Button2的CommandName屬性設定了值,而這個值等同於GridView中按鈕的命令,當我按下時,則會去處發GridView中的RowCommand事件;而另外一個Button1則是普通的Click事件;我將針對著兩個Button做個別的程式撰寫,但效果都是一樣的。
..................
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "ShowData" Then
            Dim Bu1 As Button = CType(e.CommandSource, Button) '先取得命令的來源並轉換成按鈕
            Dim GV_Row As GridViewRow = CType(Bu1.NamingContainer, GridViewRow) '將Button轉換成GridViewRow就是您所點選的某一列

            MsgBox(CType(GV_Row.FindControl("Button2"), Button).ClientID & ";" & GV_Row.Cells(1).Text & ";" & GV_Row.Cells(2).Text)
        End If
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Bu2 As Button = CType(sender, Button) '直接將Button1_Click事件中的sender參數轉換成按鈕
        Dim GV_Row As GridViewRow = CType(Bu2.NamingContainer, GridViewRow) '將Button轉換成GridViewRow就是您所點選的某一列

        MsgBox(CType(GV_Row.FindControl("Button1"), Button).ClientID & ";" & GV_Row.Cells(1).Text & ";" & GV_Row.Cells(2).Text)
    End Sub
..................

執行結果:












說明:
1.MsgBox是在VB.NET裡才有的東西,如果想在網頁中顯示訊息框,那您得使用JavaScript等其他方法來顯示。
2.CType(GV_Row.FindControl("Button1"), Button).ClientID 這段的意思是我將抓到的Button1轉換成Button,轉換完後取得他的Client ID,有些控制項經過網頁編譯後他就不是程式碼中原本的ID。
3.那既然可以寫Button_Click事件裡,為什麼還要寫在GridView的RowCommand事件呢?
寫在RowCommand事件的好處就是,當你有為GridView設定CommandArgument屬性時,我就可以透過Dim str_pk As String = CStr(e.CommandArgument)這行程式碼很容易的取得資料表的主鍵

P.S如有錯誤請指正,要是有其他問題請自行搜尋。

3 則留言:

  1. 您好,請問針對這個功能,可以只取得某一列的某一欄的值就好了嗎? 例如我只想取得姓名這個欄位的值

    回覆刪除
  2. 我是用c#開發的 可以的話 可以請前輩幫我寫這段程式碼嗎?謝謝您^^"

    回覆刪除
    回覆
    1. Button myButton = (Button)e.CommandSource;
      GridViewRow myRow = (GridViewRow)myButton.NamingContainer;

      資料來原:http://www.dotblogs.com.tw/mis2000lab/archive/2011/09/08/gridview_selectedindex_dataitemindex_rowcommand_2011.aspx

      建議您先找一本書來看,我推薦的這位作者的書。

      刪除