2009-04-22 34 views
3

目前我有一个附加到GridView的DetailsView。当您从GridView中选择一个项目时,细节将显示在DetailsView(duh)中。现在,DetailsView上有3个事件选项:编辑,删除和新建。我想添加一个第4格式。这将简单地运行一些查询,然后执行删除。这是可能的和如何?我可以向ASP.NET中的DetailsView添加其他操作吗?

另一种可能性是添加这个事件到GridView,而不是(在编辑模式下也许?)

回答

5

如果您添加自己的按钮,DetailsView控件为建议你可以在DetailsView挂钩ItemCommand事件,并添加ItemCommand方法中每个CommandName的任何逻辑

<%@ 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> 
相关问题