WebSurfer's Home

""

GirdView 中のクリックされた Button を取得

GridView の中に複数あるボタンのどれがクリックされたかを取得します。 ボタンをクリックすると、そのボタンのある GridView の ID、GridView の RowIndex、ボタンの ID を Label に表示します。


コード商品名単価   
0Item 0123
1Item 1246
2Item 2369
3Item 3492
4Item 4615
5Item 5738
6Item 6861
7Item 7984
8Item 81107


コードは以下のとおりです。

<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> DataTable CreateDataTable() { DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new DataColumn("Code", typeof(Int32))); dt.Columns.Add(new DataColumn("Name", typeof(String))); dt.Columns.Add(new DataColumn("Price", typeof(Int32))); for (int i = 0; i < 9; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Item " + i.ToString(); dr[2] = 123 * (i + 1); dt.Rows.Add(dr); } return dt; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = CreateDataTable(); GridView1.DataBind(); } } protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { ((Button)e.Row.FindControl("Button1")).CommandArgument = e.Row.RowIndex.ToString(); ((Button)e.Row.FindControl("Button2")).CommandArgument = e.Row.RowIndex.ToString(); ((Button)e.Row.FindControl("Button3")).CommandArgument = e.Row.RowIndex.ToString(); } } protected void WriteLabel(string grid, string index, string btn) { Label1.Text = String.Format("{0}, RowIndex{1}, {2} clicked", grid, index, btn); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "Button1": WriteLabel(((GridView)sender).ID, (string)e.CommandArgument, e.CommandName); break; case "Button2": WriteLabel(((GridView)sender).ID, (string)e.CommandArgument, e.CommandName); break; case "Button3": WriteLabel(((GridView)sender).ID, (string)e.CommandArgument, e.CommandName); break; default: WriteLabel(((GridView)sender).ID, (string)e.CommandArgument, e.CommandName); break; } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcreated="GridView1_RowCreated" onrowcommand="GridView1_RowCommand"> <Columns> <asp:BoundField DataField="Code" HeaderText="コード" /> <asp:BoundField DataField="Name" HeaderText="商品名" /> <asp:BoundField DataField="Price" HeaderText="単価" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button1" runat="server" CommandName="Button1" Text="Button1" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button2" runat="server" CommandName="Button2" Text="Button2" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button3" runat="server" CommandName="Button3" Text="Button3" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <br /> <div> <asp:Label ID="Label1" runat="server"></asp:Label> </div> </form> </body> </html>