CommandField.ShowInsertButton Propiedad

Definición

Obtiene o establece un valor que indica si en un campo CommandField se muestra un botón de nuevo.

public:
 virtual property bool ShowInsertButton { bool get(); void set(bool value); };
public virtual bool ShowInsertButton { get; set; }
member this.ShowInsertButton : bool with get, set
Public Overridable Property ShowInsertButton As Boolean

Valor de propiedad

Boolean

true para mostrar un botón de nuevo en un campo CommandField; en caso contrario, false. De manera predeterminada, es false.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar la ShowInsertButton propiedad para mostrar un botón Nuevo en un DetailsView control , lo que permite al usuario agregar un nuevo registro al origen de datos.


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

<!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>CommandField Example</title>
</head>
<body>
    <form id="form1" runat="server">
      
      <h3>CommandField Example</h3>

      <asp:detailsview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneraterows="false"
        datakeynames="CustomerID"  
        allowpaging="true" 
        runat="server">
        
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="CompanyName"/>
          <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:commandfield showinsertbutton="true"
            showheader="true"
            headertext="Add Customer"/>
        </fields>
        
      </asp:detailsview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        insertcommand="Insert Into [Customers]([CustomerID], [CompanyName], [City], [PostalCode], [Country]) Values (@CustomerID, @CompanyName, @City, @PostalCode, @Country)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </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">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>CommandField Example</title>
</head>
<body>
    <form id="form1" runat="server">
      
      <h3>CommandField Example</h3>

      <asp:detailsview id="CustomersGridView" 
        datasourceid="CustomersSqlDataSource" 
        autogeneraterows="false"
        datakeynames="CustomerID"  
        allowpaging="true" 
        runat="server">
        
        <fields>
          <asp:boundfield datafield="CustomerID"
            headertext="Customer ID" />
          <asp:boundfield datafield="CompanyName"
            headertext="CompanyName"/>
          <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:commandfield showinsertbutton="true"
            showheader="true"
            headertext="Add Customer"/>
        </fields>
        
      </asp:detailsview>
            
      <!-- 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="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        insertcommand="Insert Into [Customers]([CustomerID], [CompanyName], [City], [PostalCode], [Country]) Values (@CustomerID, @CompanyName, @City, @PostalCode, @Country)"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

Comentarios

Utilice la ShowInsertButton propiedad para especificar si se muestra un botón Nuevo en un CommandField campo. El botón Nuevo solo se muestra una vez en el CommandField campo y permite al usuario agregar un nuevo registro en el origen de datos.

Nota

Esta propiedad solo se aplica a los controles enlazados a datos que admiten operaciones de inserción, como el DetailsView control .

Cuando el usuario hace clic en el botón Nuevo, se muestran los controles de entrada para cada campo que se muestra en el control enlazado a datos, lo que permite al usuario escribir los valores del nuevo registro. El botón Nuevo se reemplaza por un botón Insertar y un botón Cancelar, y todos los demás botones de comando del CommandField campo están ocultos. Al hacer clic en el botón Insertar, se agrega el registro al origen de datos, mientras que al hacer clic en el botón Cancelar se cancela la operación.

Nota

Cuando se usa un control enlazado a datos en combinación con un control de origen de datos (por ejemplo, un SqlDataSource control), el control enlazado a datos puede aprovechar las funcionalidades del control de origen de datos y proporcionar funcionalidad de inserción automática. Para otros orígenes de datos, debe proporcionar las rutinas para realizar la operación de inserción durante el evento adecuado para el control enlazado a datos.

Cuando la ButtonType propiedad de un CommandField campo se establece ButtonType.Button en o ButtonType.Link, use la NewText propiedad para especificar el texto que se va a mostrar para un botón Nuevo. Como alternativa, puede mostrar una imagen estableciendo primero la ButtonType propiedad ButtonType.Image en y, a continuación, estableciendo la NewImageUrl propiedad .

Se aplica a

Consulte también