Export (0) Print
Expand All

DetailsViewCommandEventArgs Class

Provides data for the ItemCommand event.

System.Object
  System.EventArgs
    System.Web.UI.WebControls.CommandEventArgs
      System.Web.UI.WebControls.DetailsViewCommandEventArgs

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

'Declaration
Public Class DetailsViewCommandEventArgs _
	Inherits CommandEventArgs

The DetailsViewCommandEventArgs type exposes the following members.

  NameDescription
Public methodDetailsViewCommandEventArgsInitializes a new instance of the DetailsViewCommandEventArgs class.
Top

  NameDescription
Public propertyCommandArgumentGets the argument for the command. (Inherited from CommandEventArgs.)
Public propertyCommandNameGets the name of the command. (Inherited from CommandEventArgs.)
Public propertyCommandSourceGets the source of the command.
Public propertyHandledGets or sets a value that indicates whether the control has handled the event.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The DetailsView control raises the ItemCommand event when a button within a ButtonField, CommandField, or TemplateField row field is clicked. This allows you to provide an event handler that performs a custom routine whenever this event occurs.

NoteNote

The DetailsView control also raises other specialized events when certain buttons are clicked (buttons with the CommandName property set to "Delete", "Insert", "Page", or "Update"). When using one of these buttons, you should consider using one of the specialized events provided by the control (such as ItemDeleted or ItemDeleting).

A DetailsViewCommandEventArgs object is passed to the event handler. If the button that raised the event has a command name or command argument value, you can use the DetailsViewCommandEventArgs object to determine these values. To determine the command name and command argument of the button clicked, use the CommandName and CommandArgument properties, respectively. You can also access the DetailsView control that raised the event by using the CommandSource property.

For a list of initial property values for an instance of the DetailsViewCommandEventArgs class, see the DetailsViewCommandEventArgs constructor.

The following code example demonstrates how to use the DetailsViewCommandEventArgs object passed to the event handler for the ItemCommand event to determine the command name of the button clicked by the user. This example uses the single-file coding model.


<%@ 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>

The following code example shows a code-behind coding model version of the previous example. For this example to work, you must copy the code below into the associated code-behind file.

<%@ 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>

The following code example shows the code-behind file for the previous example.

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft