2009-04-22 6 views
3

Actuellement, j'ai un DetailsView attaché à un GridView. Lorsque vous sélectionnez un élément dans GridView, les détails s'affichent dans DetailsView (duh). À l'heure actuelle, il y a 3 options d'événement sur DetailsView: Modifier, Supprimer et Nouveau. Je voudrais ajouter un 4ème, format. Cela va simplement exécuter quelques requêtes et ensuite effectuer une suppression. Est-ce possible et comment?Puis-je ajouter une action supplémentaire à DetailsView dans ASP.NET?

Une autre possibilité serait d'ajouter cet événement à la GridView place (peut-être en mode Edit?)

Répondre

5

Si vous ajoutez vos propres boutons à la DetailsView comme le suggère que vous pouvez brancher l'événement ItemCommand sur le DetailsView et ajouter toute logique pour chaque CommandName dans la méthode ItemCommand

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server"> 

    void CustomerDetailView_ItemCommand(Object sender, DetailsViewCommandEventArgs e) 
    { 

     // Use the CommandName property to determine which button 
     // was clicked. 
     if (e.CommandName == "Add") 
     { 

      // 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. 
      DetailsViewRow row = 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. 
      String name = row.Cells[1].Text; 

      // Create a ListItem object with the store's name. 
      ListItem item = new ListItem(name); 

      // Add the ListItem object to the ListBox, if the 
      // item doesn't already exist. 
      if (!ContactListBox.Items.Contains(item)) 
      { 
       ContactListBox.Items.Add(item); 
      } 

     } 

    } 

</script> 

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