DetailsViewCommandEventHandler 代理人

定義

表示處理 ItemCommand 控制項之 DetailsView 事件的方法。

public delegate void DetailsViewCommandEventHandler(System::Object ^ sender, DetailsViewCommandEventArgs ^ e);
public delegate void DetailsViewCommandEventHandler(object sender, DetailsViewCommandEventArgs e);
type DetailsViewCommandEventHandler = delegate of obj * DetailsViewCommandEventArgs -> unit
Public Delegate Sub DetailsViewCommandEventHandler(sender As Object, e As DetailsViewCommandEventArgs)

參數

sender
Object

事件的來源。

範例

下列程式碼範例示範如何以程式設計方式將委派新增 DetailsViewCommandEventHandlerItemCommand 控制項的事件 DetailsView 。 此範例使用單一檔案編碼模型。


<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new DetailsView object.
    DetailsView customerDetailsView = new DetailsView();

    // Set the DetailsView object's properties.
    customerDetailsView.ID = "CustomerDetailsView";
    customerDetailsView.DataSourceID = "DetailsViewSource";
    customerDetailsView.AutoGenerateRows = true;
    customerDetailsView.AllowPaging = true;
    customerDetailsView.DataKeyNames = new String[1] { "CustomerID" };

    // Add a button field to the DetailsView control.
    ButtonField field = new ButtonField();
    field.ButtonType = ButtonType.Link;
    field.CausesValidation = false;
    field.Text = "Add to List";
    field.CommandName="Add";

    customerDetailsView.Fields.Add(field);

    // Programmatically register the event-handling method
    // for the ItemDeleting event of a DetailsView control.
    customerDetailsView.ItemCommand 
      += new DetailsViewCommandEventHandler( 
      this.CustomerDetailsView_ItemCommand);

    // Add the DetailsView object to the Controls collection
    // of the PlaceHolder control.
    DetailsViewPlaceHolder.Controls.Add(customerDetailsView);

  }
  
  void CustomerDetailsView_ItemCommand(Object sender, 
    DetailsViewCommandEventArgs e)
  {

    // Use the CommandName property to determine which button
    // was clicked. 
    if (e.CommandName == "Add")
    {
      // Get the DetailsView control that raised the event.
      DetailsView customerDetailsView = (DetailsView)e.CommandSource;

      // Add the current customer to the customer list. 

      // Get the row that contains the company name. In this
      // example, the company name is in the second row (index 1)  
      // of the DetailsView control.
      DetailsViewRow row = customerDetailsView.Rows[1];

      // Get the company's name from the appropriate cell.
      // In this example, the company name is in the second cell  
      // (index 1) of the row.
      String name = row.Cells[1].Text;

      // Create a ListItem object with the company name.
      ListItem item = new ListItem(name);

      // Add the ListItem object to the ListBox, if the 
      // item doesn't already exist.
      if (!CustomerListBox.Items.Contains(item))
      {
        CustomerListBox.Items.Add(item);
      }

    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventHandler Example</h3>
      
      <!-- Use a PlaceHolder control as the container for the -->
      <!-- dynamically generated DetailsView control.         -->       
      <asp:placeholder id="DetailsViewPlaceHolder"
        runat="server"/>
      
      <br/><br/>
      
      Selected Customers:<br/>
      <asp:listbox id="CustomerListBox"
        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"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 

        runat="server"/>  
  
    </form>
  </body>
</html>

<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' Create a new DetailsView object.
    Dim customerDetailsView As New DetailsView()

    ' Set the DetailsView object's properties.
    customerDetailsView.ID = "CustomerDetailsView"
    customerDetailsView.DataSourceID = "DetailsViewSource"
    customerDetailsView.AutoGenerateRows = True
    customerDetailsView.AllowPaging = True
    
    Dim keyArray() As String = {"CustomerID"}
    customerDetailsView.DataKeyNames = keyArray
    
    ' Add a button field to the DetailsView control.
    Dim field As New ButtonField()
    field.ButtonType = ButtonType.Link
    field.CausesValidation = False
    field.Text = "Add to List"
    field.CommandName = "Add"

    customerDetailsView.Fields.Add(field)

    ' Programmatically register the event-handling method
    ' for the ItemDeleting event of a DetailsView control.
    AddHandler customerDetailsView.ItemCommand, _
      AddressOf CustomerDetailsView_ItemCommand

    ' Add the DetailsView object to the Controls collection
    ' of the PlaceHolder control.
    DetailsViewPlaceHolder.Controls.Add(customerDetailsView)

  End Sub
  
  Sub CustomerDetailsView_ItemCommand(ByVal sender As Object, _
    ByVal e As DetailsViewCommandEventArgs)

    ' Use the CommandName property to determine which button
    ' was clicked. 
    If e.CommandName = "Add" Then

      ' Get the DetailsView control that raised the event.
      Dim customerDetailsView As DetailsView = _
        CType(e.CommandSource, DetailsView)

      ' Add the current customer to the customer list. 

      ' Get the row that contains the company name. In this
      ' example, the company name is in the second row (index 1)  
      ' of the DetailsView control.
      Dim row As DetailsViewRow = customerDetailsView.Rows(1)

      ' Get the company's name from the appropriate cell.
      ' In this example, the company 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 company name.
      Dim item As New ListItem(name)

      ' Add the ListItem object to the ListBox, if the 
      ' item doesn't already exist.
      If Not CustomerListBox.Items.Contains(item) Then
      
        CustomerListBox.Items.Add(item)
        
      End If

    End If

  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventHandler Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventHandler Example</h3>
      
      <!-- Use a PlaceHolder control as the container for the -->
      <!-- dynamically generated DetailsView control.         -->       
      <asp:placeholder id="DetailsViewPlaceHolder"
        runat="server"/>
      
      <br/><br/>
      
      Selected Customers:<br/>
      <asp:listbox id="CustomerListBox"
        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"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring=
            "<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
  
    </form>
  </body>
</html>

下列程式碼範例顯示先前範例的程式碼後置編碼模型版本。 若要讓這個範例運作,您必須將下列程式碼複製到相關聯的程式碼後置檔案。

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventHandler Example</title>
</head>
<body>
    <form id="Form1" runat="server">
    
      <h3>DetailsViewCommandEventHandler Example</h3>
      
      <!-- Use a PlaceHolder control as the container for the -->
      <!-- dynamically generated DetailsView control.         -->       
      <asp:placeholder id="DetailsViewPlaceHolder"
        runat="server"/>
      
      <br/><br/>
      
      Selected Customers:<br/>
      <asp:listbox id="CustomerListBox"
        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"
        selectcommand="Select [CustomerID], [CompanyName], [Address], 
          [City], [PostalCode], [Country] From [Customers]"
        connectionstring="
          <%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
  
    </form>
  </body>
</html>
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="DefaultVB.aspx.vb" Inherits="DefaultVB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventHandler Example</title>
</head>
<body>
    <form id="Form1" runat="server">
    
      <h3>DetailsViewCommandEventHandler Example</h3>
      
      <!-- Use a PlaceHolder control as the container for the -->
      <!-- dynamically generated DetailsView control.         -->       
      <asp:placeholder id="DetailsViewPlaceHolder"
        runat="server"/>
      
      <br/><br/>
      
      Selected Customers:<br/>
      <asp:listbox id="CustomerListBox"
        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"
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 
        runat="server"/>  
  
    </form>
  </body>
</html>

下列程式碼範例顯示上一個範例的程式碼後置檔案。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Create a new DetailsView object.
        DetailsView customerDetailsView = new DetailsView();

        // Set the DetailsView object's properties.
        customerDetailsView.ID = "CustomerDetailsView";
        customerDetailsView.DataSourceID = "DetailsViewSource";
        customerDetailsView.AutoGenerateRows = true;
        customerDetailsView.AllowPaging = true;
        customerDetailsView.DataKeyNames = new String[1] { "CustomerID" };

        // Add a button field to the DetailsView control.
        ButtonField field = new ButtonField();
        field.ButtonType = ButtonType.Link;
        field.CausesValidation = false;
        field.Text = "Add to List";
        field.CommandName = "Add";

        customerDetailsView.Fields.Add(field);

        // Programmatically register the event-handling method
        // for the ItemDeleting event of a DetailsView control.
        customerDetailsView.ItemCommand += new DetailsViewCommandEventHandler(this.CustomerDetailsView_ItemCommand);

        // Add the DetailsView object to the Controls collection
        // of the PlaceHolder control.
        DetailsViewPlaceHolder.Controls.Add(customerDetailsView);
    }

    void CustomerDetailsView_ItemCommand(Object sender, DetailsViewCommandEventArgs e)
    {

        // Use the CommandName property to determine which button
        // was clicked. 
        if (e.CommandName == "Add")
        {
            // Get the DetailsView control that raised the event.
            DetailsView customerDetailsView = (DetailsView)sender;

            // Add the current customer to the customer list. 

            // Get the row that contains the company name. In this
            // example, the company name is in the second row (index 1)  
            // of the DetailsView control.
            DetailsViewRow row = customerDetailsView.Rows[1];

            // Get the company's name from the appropriate cell.
            // In this example, the company name is in the second cell  
            // (index 1) of the row.
            String name = row.Cells[1].Text;

            // Create a ListItem object with the company name.
            ListItem item = new ListItem(name);

            // Add the ListItem object to the ListBox, if the 
            // item doesn't already exist.
            if (!CustomerListBox.Items.Contains(item))
            {
                CustomerListBox.Items.Add(item);
            }
        }
    }
}
Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        ' Create a new DetailsView object.
        Dim customerDetailsView As New DetailsView()

        ' Set the DetailsView object's properties.
        customerDetailsView.ID = "CustomerDetailsView"
        customerDetailsView.DataSourceID = "DetailsViewSource"
        customerDetailsView.AutoGenerateRows = True
        customerDetailsView.AllowPaging = True

        Dim keyArray() As String = {"CustomerID"}
        customerDetailsView.DataKeyNames = keyArray

        ' Add a button field to the DetailsView control.
        Dim field As New ButtonField()
        field.ButtonType = ButtonType.Link
        field.CausesValidation = False
        field.Text = "Add to List"
        field.CommandName = "Add"

        customerDetailsView.Fields.Add(field)

        ' Programmatically register the event-handling method
        ' for the ItemDeleting event of a DetailsView control.
        AddHandler customerDetailsView.ItemCommand, AddressOf CustomerDetailsView_ItemCommand

        ' Add the DetailsView object to the Controls collection
        ' of the PlaceHolder control.
        DetailsViewPlaceHolder.Controls.Add(customerDetailsView)

    End Sub

    Sub CustomerDetailsView_ItemCommand(ByVal sender As Object, ByVal e As DetailsViewCommandEventArgs)

        ' Use the CommandName property to determine which button
        ' was clicked. 
        If e.CommandName = "Add" Then

            ' Get the DetailsView control that raised the event.
            Dim customerDetailsView As DetailsView = CType(sender, DetailsView)

            ' Add the current customer to the customer list. 

            ' Get the row that contains the company name. In this
            ' example, the company name is in the second row (index 1)  
            ' of the DetailsView control.
            Dim row As DetailsViewRow = customerDetailsView.Rows(1)

            ' Get the company's name from the appropriate cell.
            ' In this example, the company 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 company name.
            Dim item As New ListItem(name)

            ' Add the ListItem object to the ListBox, if the 
            ' item doesn't already exist.
            If Not CustomerListBox.Items.Contains(item) Then

                CustomerListBox.Items.Add(item)

            End If

        End If

    End Sub

End Class

下列程式碼範例示範如何以宣告方式將委派新增 DetailsViewCommandEventHandlerItemCommand 控制項的事件 DetailsView


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>
            DetailsView ItemCommand Example</title>
</head>
<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="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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 xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>
            DetailsView ItemCommand Example</title>
</head>
<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>

備註

按一下 、 CommandFieldTemplateField 資料欄欄位內的 ButtonField 按鈕時,控制項 DetailsViewItemCommand 引發 事件。 這可讓您提供事件處理常式,以在發生此事件時執行自訂常式。

注意

DetailsView當按一下特定按鈕時,控制項也會引發其他特製化事件 (按鈕 CommandName ,且屬性設定為 「Delete」、「Insert」、「Page」 或 「Update」) 。 使用其中一個按鈕時,您應該考慮使用控制項所提供的其中一個特製化事件 (,例如 ItemDeletedItemDeleting) 。

建立 DetailsViewCommandEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理常式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱