DetailsView.ItemCommand イベント
アセンブリ: System.Web (system.web.dll 内)

Dim instance As DetailsView Dim handler As DetailsViewCommandEventHandler AddHandler instance.ItemCommand, handler
public: event DetailsViewCommandEventHandler^ ItemCommand { void add (DetailsViewCommandEventHandler^ value); void remove (DetailsViewCommandEventHandler^ value); }

ItemCommand イベントは、DetailsView コントロール内のボタン コントロールがクリックされた場合に発生します。これにより、このイベントが発生するたびにカスタム ルーチンを実行するイベント ハンドラを提供できます。
DetailsViewCommandEventArgs オブジェクトがイベント ハンドラに渡され、これによりクリックされたボタンのコマンド名およびコマンド引数を確認できます。現在のレコードのインデックスを確認するには、PageIndex プロパティを使用します。

ItemCommand イベントを使用して、DetailsView コントロールのボタン クリックを処理する方法のコード例を次に示します。ユーザーが Add Contact ボタンをクリックすると、項目が ListBox コントロールに追加されます。
<%@ Page Language="VB" %> <script runat="server"> Sub CustomerDetailView_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs) ' Use the CommandName property to determine which button ' was clicked. If e.CommandName = "Add" Then ' Add the current store to the contact list. ' Get the row that contains the store name. In this ' example, the store name is in the second row (index 1) ' of the DetailsView control. Dim row As DetailsViewRow = CustomerDetailView.Rows(1) ' Get the store's name from the appropriate cell. ' In this example, the store name is in the second cell ' (index 1) of the row. Dim name As String = row.Cells(1).Text ' Create a ListItem object with the store's name. Dim item As New ListItem(name) ' Add the ListItem object to the ListBox, if the ' item doesn't already exist. If Not ContactListBox.Items.Contains(item) Then ContactListBox.Items.Add(item) End If End If End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3> DetailsView ItemCommand Example</h3> <asp:DetailsView ID="CustomerDetailView" DataSourceID="DetailsViewSource" AutoGenerateRows="false" DataKeyNames="CustomerID" AllowPaging="true" OnItemCommand="CustomerDetailView_ItemCommand" runat="server"> <FieldHeaderStyle BackColor="Navy" ForeColor="White" /> <Fields> <asp:BoundField DataField="CustomerID" HeaderText="Store ID" /> <asp:BoundField DataField="CompanyName" HeaderText="Store Name" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:ButtonField CommandName="Add" Text="Add Contact" /> </Fields> </asp:DetailsView> <hr /> Contacts:<br /> <asp:ListBox ID="ContactListBox" runat="server" /> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:SqlDataSource ID="DetailsViewSource" runat="server" ConnectionString= "<%$ ConnectionStrings:NorthWindConnectionString%>" InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)" SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"> </asp:SqlDataSource> </form> </body> </html>
<%@ Page Language="C#" %> <script runat="server"> void CustomerDetailView_ItemCommand(Object sender, DetailsViewCommandEventArgs e) { // Use the CommandName property to determine which button // was clicked. if (e.CommandName == "Add") { // Add the current store to the contact list. // Get the row that contains the store name. In this // example, the store name is in the second row (index 1) // of the DetailsView control. DetailsViewRow row = CustomerDetailView.Rows[1]; // Get the store's name from the appropriate cell. // In this example, the store name is in the second cell // (index 1) of the row. String name = row.Cells[1].Text; // Create a ListItem object with the store's name. ListItem item = new ListItem(name); // Add the ListItem object to the ListBox, if the // item doesn't already exist. if (!ContactListBox.Items.Contains(item)) { ContactListBox.Items.Add(item); } } } </script> <html> <body> <form id="Form1" runat="server"> <h3> DetailsView ItemCommand Example</h3> <asp:DetailsView ID="CustomerDetailView" DataSourceID="DetailsViewSource" AutoGenerateRows="false" DataKeyNames="CustomerID" AllowPaging="true" OnItemCommand="CustomerDetailView_ItemCommand" runat="server"> <FieldHeaderStyle BackColor="Navy" ForeColor="White" /> <Fields> <asp:BoundField DataField="CustomerID" HeaderText="Store ID" /> <asp:BoundField DataField="CompanyName" HeaderText="Store Name" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:ButtonField CommandName="Add" Text="Add Contact" /> </Fields> </asp:DetailsView> <hr /> Contacts:<br /> <asp:ListBox ID="ContactListBox" runat="server" /> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the web.config file. --> <asp:SqlDataSource ID="DetailsViewSource" runat="server" ConnectionString= "<%$ ConnectionStrings:NorthWindConnectionString%>" InsertCommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)" SelectCommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"> </asp:SqlDataSource> </form> </body> </html>

Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDetailsView.ItemCommand イベントを検索する場合は、下記のリンクをクリックしてください。

- DetailsView.ItemCommand イベントのページへのリンク