DetailsViewCommandEventArgs 類別

定義

提供 ItemCommand 事件的資料。

public ref class DetailsViewCommandEventArgs : System::Web::UI::WebControls::CommandEventArgs
public class DetailsViewCommandEventArgs : System.Web.UI.WebControls.CommandEventArgs
type DetailsViewCommandEventArgs = class
    inherit CommandEventArgs
Public Class DetailsViewCommandEventArgs
Inherits CommandEventArgs
繼承
DetailsViewCommandEventArgs

範例

下列程式碼範例示範如何使用 DetailsViewCommandEventArgs 傳遞給事件的事件處理常式 ItemCommand 的物件,來判斷使用者所按一下按鈕的命令名稱。 此範例使用單一檔案編碼模型。


<%@ 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 ItemDetailsView_ItemCommand(Object sender, 
      DetailsViewCommandEventArgs e)
  {

    // Use the CommandName property to determine which button
    // was clicked. 
    if (e.CommandName == "Add")
    {

      // Add the 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 = ItemDetailsView.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 control, if the 
      // item does not already exist.
      if (!CustomerListBox.Items.Contains(item))
      {
        CustomerListBox.Items.Add(item);
      }

    }

  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsViewCommandEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false" 
        onitemcommand="ItemDetailsView_ItemCommand"  
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:buttonfield buttontype="Link"
            causesvalidation="false"
            text="Add to List"
            commandname="Add"/>
        </fields>
      </asp:detailsview>
      
      <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" %>

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

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

      ' Add the 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 = ItemDetailsView.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 control, if the 
      ' item does not 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>DetailsViewCommandEventArgs Example</title>
</head>
<body>
    <form id="form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false"  
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:buttonfield buttontype="Link"
            causesvalidation="false"
            text="Add to List"
            commandname="Add"/>
        </fields>
      </asp:detailsview>
      
      <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>DetailsViewCommandEventArgs Example</title>
</head>
<body>
    <form id="Form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false" 
        onitemcommand="ItemDetailsView_ItemCommand"  
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:buttonfield buttontype="Link"
            causesvalidation="false"
            text="Add to List"
            commandname="Add"/>
        </fields>
      </asp:detailsview>
      
      <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>DetailsViewCommandEventArgs Example</title>
</head>
<body>
    <form id="Form1" runat="server">
    
      <h3>DetailsViewCommandEventArgs Example</h3>
  
      <asp:detailsview id="ItemDetailsView"
        datasourceid="DetailsViewSource"
        allowpaging="true"
        autogeneraterows="false" 
        onitemcommand="ItemDetailsView_ItemCommand"  
        runat="server">
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID"/>
          <asp:boundfield datafield="CompanyName"
            headertext="Company Name"/>
          <asp:boundfield datafield="Address"
            headertext="Address"/>
          <asp:boundfield datafield="City"
            headertext="City"/>
          <asp:boundfield datafield="PostalCode"
            headertext="ZIP Code"/>
          <asp:boundfield datafield="Country"
            headertext="Country"/>
          <asp:buttonfield buttontype="Link"
            causesvalidation="false"
            text="Add to List"
            commandname="Add"/>
        </fields>
      </asp:detailsview>
      
      <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.Collections;
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
{
    public void ItemDetailsView_ItemCommand(Object sender, DetailsViewCommandEventArgs e)
    {

        // Use the CommandName property to determine which button
        // was clicked. 
        if (e.CommandName == "Add")
        {

            // Add the 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 = ItemDetailsView.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 control, if the 
            // item does not already exist.
            if (!CustomerListBox.Items.Contains(item))
            {
                CustomerListBox.Items.Add(item);
            }
        }
    }
}
Partial Class DefaultVB
    Inherits System.Web.UI.Page

    Public Sub ItemDetailsView_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 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 = ItemDetailsView.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 ListItem = New ListItem(name)

            ' Add the ListItem object to the ListBox control, if the 
            ' item does not already exist.
            If Not CustomerListBox.Items.Contains(item) Then
                CustomerListBox.Items.Add(item)
            End If
        End If
    End Sub

End Class

備註

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

注意

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

DetailsViewCommandEventArgs物件會傳遞至事件處理常式。 如果引發事件的按鈕具有命令名稱或命令引數值,您可以使用 DetailsViewCommandEventArgs 物件來判斷這些值。 若要判斷按下按鈕的命令名稱和命令引數,請分別使用 CommandNameCommandArgument 屬性。 您也可以使用 CommandSource 屬性來存取 DetailsView 引發事件的控制項。

如需 DetailsViewCommandEventArgs 類別之執行個體的初始屬性值清單,請參閱 DetailsViewCommandEventArgs 建構函式。

建構函式

DetailsViewCommandEventArgs(Object, CommandEventArgs)

初始化 DetailsViewCommandEventArgs 類別的新執行個體。

屬性

CommandArgument

取得命令的引數。

(繼承來源 CommandEventArgs)
CommandName

取得命令的名稱。

(繼承來源 CommandEventArgs)
CommandSource

取得命令的來源。

Handled

取得或設定值,指出控制項是否已處理事件。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

另請參閱