DetailsViewInsertEventArgs.CommandArgument Property


Gets the command argument for the insert operation passed to the DetailsView control.

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

public object CommandArgument { get; }

Property Value

Type: System.Object

The command argument for the insert operation passed to the DetailsView control.

Use the CommandArgument property to determine the value of the command argument passed to the DetailsView control. The CommandArgument property is used to carry extra information about insertion.

The following code example demonstrates how to set the CommandArgument property of a Button control and retrieve and use the value during the ItemInserting event.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void CustomerDetailsView_ItemInserting(Object sender, DetailsViewInsertEventArgs e)
    // Use the Values property to retrieve the key field value.
    String keyValue = e.Values["CustomerID"].ToString();

    if (((string)(e.CommandArgument)) == "CheckID")
        // Insert the record only if the key field is four characters
        // long; otherwise, cancel the insert operation.
        if (keyValue.Length == 4)
            // Change the key field value to upper case before inserting 
            // the record in the data source.
            e.Values["CustomerID"] = keyValue.ToUpper();

            MessageLabel.Text = "";
            MessageLabel.Text = "The key field must have four digits.";
            e.Cancel = true;


<html xmlns="" >
  <head runat="server">
    <title>DetailsViewInsertEventArgs Example</title>
    <form id="Form1" runat="server">

      <h3>DetailsViewInsertEventArgs Example</h3>

        <asp:detailsview id="CustomerDetailsView"
          allowpaging="true" AutoGenerateInsertButton="true"

                <asp:TemplateField >
                    <asp:Button CommandName="Insert" CommandArgument="CheckID" 
                      runat="server" Text="Insert with ID Check" BackColor="green" />

        <asp:label id="MessageLabel"

        <!-- 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]"
          insertcommand="INSERT INTO [Customers]([CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country]) VALUES (@CustomerID, @CompanyName, @Address, @City, @PostalCode, @Country)"
          connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" 


.NET Framework
Available since 2.0
Return to top