Condividi tramite


CommandField.ShowInsertButton Proprietà

Definizione

Ottiene o imposta un valore che indica se un pulsante Nuovo viene visualizzato in un campo CommandField.

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

Valore della proprietà

Boolean

true per visualizzare un pulsante Nuovo in un campo CommandField; in caso contrario, false. Il valore predefinito è false.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare la ShowInsertButton proprietà per visualizzare un pulsante New in un DetailsView controllo , che consente all'utente di aggiungere un nuovo record all'origine dati.


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

Commenti

Utilizzare la ShowInsertButton proprietà per specificare se un pulsante Nuovo viene visualizzato in un CommandField campo. Il pulsante Nuovo viene visualizzato una sola volta nel CommandField campo e consente all'utente di aggiungere un nuovo record nell'origine dati.

Nota

Questa proprietà si applica solo ai controlli associati a dati che supportano operazioni di inserimento, ad esempio il DetailsView controllo .

Quando l'utente fa clic sul pulsante Nuovo, i controlli di input vengono visualizzati per ogni campo visualizzato nel controllo associato a dati, consentendo all'utente di immettere i valori per il nuovo record. Il pulsante Nuovo viene sostituito con un pulsante Inserisci e un pulsante Annulla e tutti gli altri pulsanti di comando nel CommandField campo sono nascosti. Facendo clic sul pulsante Inserisci viene aggiunto il record all'origine dati, mentre facendo clic sul pulsante Annulla viene annullata l'operazione.

Nota

Quando un controllo associato a dati viene usato in combinazione con un controllo origine dati ,ad esempio un SqlDataSource controllo , il controllo associato a dati può sfruttare le funzionalità del controllo origine dati e fornire funzionalità di inserimento automatico. Per altre origini dati, è necessario specificare le routine per eseguire l'operazione di inserimento durante l'evento appropriato per il controllo associato a dati.

Quando la ButtonType proprietà di un CommandField campo è impostata su ButtonType.Button o ButtonType.Link, utilizzare la NewText proprietà per specificare il testo da visualizzare per un pulsante Nuovo. In alternativa, è possibile visualizzare un'immagine impostando prima la ButtonType proprietà su ButtonType.Image e quindi impostando la NewImageUrl proprietà .

Si applica a

Vedi anche