This documentation is archived and is not being maintained.

DetailsViewCommandEventHandler Delegate

Represents the method that handles the ItemCommand event of a DetailsView control.

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

'Declaration
Public Delegate Sub DetailsViewCommandEventHandler ( _
	sender As Object, _
	e As DetailsViewCommandEventArgs _
)
'Usage
Dim instance As New DetailsViewCommandEventHandler(AddressOf HandlerMethod)

Parameters

sender
Type: System.Object

The source of the event.

e
Type: System.Web.UI.WebControls.DetailsViewCommandEventArgs

A DetailsViewCommandEventArgs that contains the event data.

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).

When you create a DetailsViewCommandEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.

The following code example demonstrates how to programmatically add a DetailsViewCommandEventHandler delegate to the ItemCommand event of a DetailsView control. This example uses the single-file coding model.

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

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

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

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

The following code example demonstrates how to declaratively add a DetailsViewCommandEventHandler delegate to the ItemCommand event of a DetailsView control.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: