共用方式為


DetailsView.ItemCreated 事件

定義

DetailsView 控制項中建立記錄時會發生這個事件。

public:
 event EventHandler ^ ItemCreated;
public event EventHandler ItemCreated;
member this.ItemCreated : EventHandler 
Public Custom Event ItemCreated As EventHandler 

事件類型

範例

下列程式碼範例示範如何使用 ItemCreated 事件,以目前的分頁統計資料來更新自訂分頁器資料列。


<%@ 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_ItemCreated(Object sender, EventArgs e)
  {
    
    // Get the header row.
      DetailsViewRow headerRow = CustomerDetailView.HeaderRow;

    // Get the Label control that displays the current page information 
    // from the header row.
    Label pageNum = (Label)headerRow.FindControl("PageNumberLabel");

    if(pageNum != null)
    {
      // Update the Label control with the current page number.
        int page = CustomerDetailView.DataItemIndex + 1;
      pageNum.Text = "Page " + page.ToString ();
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView HeaderTemplate Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView HeaderTemplate Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          allowpaging="true"
          onitemcreated="CustomerDetailView_ItemCreated"   
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
            
          <pagersettings Mode="NextPreviousFirstLast"/>  
            
          <headertemplate>
          
            <table width="100%">            
              <tr>
                <td align="left">
                  <asp:Image id="LogoImage"
                    imageurl="~\images\Logo.jpg"
                    AlternateText="Our logo" 
                    runat="server"/>
                </td>
                <td align="right" valign="bottom">
                  <asp:Label id="PageNumberLabel"
                    font-size="9"
                    forecolor="DodgerBlue"
                    runat="server"/>
                </td>
              </tr>            
            </table>
          
          </headertemplate>
                    
        </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="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>

<%@ Page language="VB" %>

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

    Sub CustomerDetailView_ItemCreated(ByVal sender As Object, _
        ByVal e As EventArgs)
        ' Get the header row.
        Dim headerRow As DetailsViewRow = CustomerDetailView.HeaderRow

        ' Get the Label control that displays the current page information 
        ' from the header row.
        Dim pageNum As Label = _
            CType(headerRow.FindControl("PageNumberLabel"), Label)

        If Not pageNum Is Nothing Then
    
            ' Update the Label control with the current page number.
            Dim page As Integer = CustomerDetailView.DataItemIndex + 1
            pageNum.Text = "Page " & page.ToString()
    
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>DetailsView HeaderTemplate Example</title>
</head>
<body>
    <form id="Form1" runat="server">
        
      <h3>DetailsView HeaderTemplate Example</h3>
                
        <asp:detailsview id="CustomerDetailView"
          datasourceid="DetailsViewSource"
          datakeynames="CustomerID"
          autogeneraterows="true"
          allowpaging="true"
          onitemcreated="CustomerDetailView_ItemCreated"   
          runat="server">
               
          <headerstyle backcolor="Navy"
            forecolor="White"/>
            
          <pagersettings Mode="NextPreviousFirstLast"/>  
            
          <headertemplate>
          
            <table width="100%">            
              <tr>
                <td align="left">
                  <asp:Image id="LogoImage"
                    imageurl="~\images\Logo.jpg"
                    AlternateText="Our logo" 
                    runat="server"/>
                </td>
                <td align="right" valign="bottom">
                  <asp:Label id="PageNumberLabel"
                    font-size="9"
                    forecolor="DodgerBlue"
                    runat="server"/>
                </td>
              </tr>            
            </table>
          
          </headertemplate>
                    
        </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="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>

備註

ItemCreated每次在 控制項中 DetailsView 建立記錄時,都會引發 事件。 當控制項第一次轉譯,或使用者巡覽至另一筆記錄時,就會發生這種情況。 您可以使用這個事件來提供事件處理常式來執行自訂常式,例如,每當發生此事件時,將自訂內容新增至資料列。

您可以分別使用 HeaderRowTopPagerRowFooterRowBottomPagerRow 屬性,存取 控制項中的 DetailsView 頁首、頁尾和呼叫器資料列。 若要存取其中一個資料列,請使用 Rows 集合。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱