更新 : 2007 年 11 月
テンプレートと共にデータ バインド コントロールを使用し (DataList コントロール、FormView コントロールなど)、テンプレートに Button、LinkButton、または ImageButton の各 Web サーバー コントロールが含まれる場合、ボタンは自身の Click イベントを含まれているコントロールに転送できます。これによって、データ バインド コントロールに定義されていないカスタム機能 (編集、削除、更新、キャンセルなど) のボタンを含めることができます。
データ バインド コントロールのボタン イベントに応答するには
コントロール テンプレートに Button、LinkButton、または ImageButton を追加します。
ボタンの CommandName プロパティに、"sort" や "copy" など、機能を示す文字列を設定します。
コントロールの ItemCommand イベントのメソッドを作成します。このメソッドでは、次の処理を行います。
イベント引数オブジェクトの CommandName プロパティをチェックして、どのようなコマンド文字列が渡されたかを確認します。
ユーザーがクリックしたボタンに対して適切なロジックを実行します。
次の例では、DataList コントロールにおけるボタン クリックへの応答方法を示しています。この例では、買い物カゴを表示する ImageButton が ItemTemplate に含まれています。このボタンは AddToCart コマンドを送信します。イベント ハンドラはクリックされたボタンを特定し、それが買い物カゴ ボタンの場合は、適切なロジックを実行します。
Private Sub DataList1_ItemCommand(ByVal source As Object, _ ByVal e As DataListCommandEventArgs) _ Handles DataList1.ItemCommand If (e.CommandName = "AddToCart") Then ' Add code here to add the item to the shopping cart. ' Use the value of e.Item.ItemIndex to find the data row ' in the data source. End If End Sub
private void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "AddToCart") { // Add code here to add the item to the shopping cart. // Use the value of e.Item.ItemIndex to find the data row // in the data source. } }
参照
処理手順
方法 : ASP.NET Web ページでイベント ハンドラを作成する (Visual Studio)