2009-04-22 7 views
3

현재 GridView에 DetailsView가 연결되어 있습니다. GridView에서 항목을 선택하면 DetailsView (세부)에 세부 정보가 표시됩니다. 현재 DetailsView에는 Edit, Delete 및 New의 세 가지 이벤트 옵션이 있습니다. 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> 
관련 문제