SiteMapPath.OnItemCreated(SiteMapNodeItemEventArgs) 方法

定義

引發 ItemCreated 控制項的 SiteMapPath 事件。Raises the ItemCreated event of the SiteMapPath control.

protected:
 virtual void OnItemCreated(System::Web::UI::WebControls::SiteMapNodeItemEventArgs ^ e);
protected virtual void OnItemCreated (System.Web.UI.WebControls.SiteMapNodeItemEventArgs e);
abstract member OnItemCreated : System.Web.UI.WebControls.SiteMapNodeItemEventArgs -> unit
override this.OnItemCreated : System.Web.UI.WebControls.SiteMapNodeItemEventArgs -> unit
Protected Overridable Sub OnItemCreated (e As SiteMapNodeItemEventArgs)

參數

e
SiteMapNodeItemEventArgs

包含事件資料的 SiteMapNodeItemEventArgsA SiteMapNodeItemEventArgs that contains event data.

範例

下列程式碼範例示範如何在 OnItemCreated 方法中建立之後,呼叫方法 SiteMapNodeItem InitializeItemThe following code example demonstrates how to call the OnItemCreated method after creating a SiteMapNodeItem within the InitializeItem method. 這個程式碼範例是針對類別提供之較大範例的一部分 SiteMapPathThis code example is part of a larger example provided for the SiteMapPath class.

private void AddDropDownListAfterCurrentNode(SiteMapNodeItem item) {

    SiteMapNodeCollection childNodes = item.SiteMapNode.ChildNodes;

    // Only do this work if there are child nodes.
    if (childNodes != null) {

        // Add another PathSeparator after the CurrentNode.
        SiteMapNodeItem finalSeparator =
            new SiteMapNodeItem(item.ItemIndex,
                                SiteMapNodeItemType.PathSeparator);

        SiteMapNodeItemEventArgs eventArgs =
            new SiteMapNodeItemEventArgs(finalSeparator);

        InitializeItem(finalSeparator);
        // Call OnItemCreated every time a SiteMapNodeItem is
        // created and initialized.
        OnItemCreated(eventArgs);

        // The pathSeparator does not bind to any SiteMapNode, so
        // do not call DataBind on the SiteMapNodeItem.
        item.Controls.Add(finalSeparator);

        // Create a DropDownList and populate it with the children of the
        // CurrentNode. There are no styles or templates that are applied
        // to the DropDownList control. If OnSelectedIndexChanged is raised,
        // the event handler redirects to the page selected.
        // The CurrentNode has child nodes.
        DropDownList ddList = new DropDownList();
        ddList.AutoPostBack = true;

        ddList.SelectedIndexChanged += new EventHandler(this.DropDownNavPathEventHandler);

        // Add a ListItem to the DropDownList for every node in the
        // SiteMapNodes collection.
        foreach (SiteMapNode node in childNodes) {
            ddList.Items.Add(new ListItem(node.Title, node.Url));
        }

        item.Controls.Add(ddList);
    }
}
Private Sub AddDropDownListAfterCurrentNode(item As SiteMapNodeItem)

   Dim childNodes As SiteMapNodeCollection = item.SiteMapNode.ChildNodes

   ' Only do this work if there are child nodes.
   If Not (childNodes Is Nothing) Then

      ' Add another PathSeparator after the CurrentNode.
      Dim finalSeparator As New SiteMapNodeItem(item.ItemIndex, SiteMapNodeItemType.PathSeparator)

      Dim eventArgs As New SiteMapNodeItemEventArgs(finalSeparator)

      InitializeItem(finalSeparator)
      ' Call OnItemCreated every time a SiteMapNodeItem is
      ' created and initialized.
      OnItemCreated(eventArgs)

      ' The pathSeparator does not bind to any SiteMapNode, so
      ' do not call DataBind on the SiteMapNodeItem.
      item.Controls.Add(finalSeparator)

      ' Create a DropDownList and populate it with the children of the
      ' CurrentNode. There are no styles or templates that are applied
      ' to the DropDownList control. If OnSelectedIndexChanged is raised,
      ' the event handler redirects to the page selected.
      ' The CurrentNode has child nodes.
      Dim ddList As New DropDownList()
      ddList.AutoPostBack = True

      AddHandler ddList.SelectedIndexChanged, AddressOf Me.DropDownNavPathEventHandler

      ' Add a ListItem to the DropDownList for every node in the
      ' SiteMapNodes collection.
      Dim node As SiteMapNode
      For Each node In  childNodes
         ddList.Items.Add(New ListItem(node.Title, node.Url))
      Next node

      item.Controls.Add(ddList)
   End If
End Sub

備註

ItemCreated事件會在 SiteMapPath 控制項建立之後引發 SiteMapNodeItem ,這是代表的 Web 服務器控制項 SiteMapNode ,並將其與建立關聯 SiteMapNodeThe ItemCreated event is raised after the SiteMapPath control creates a SiteMapNodeItem, which is a Web server control that represents a SiteMapNode, and associates it to a SiteMapNode. OnItemCreated 建立的節點專案系結至其資料之前,會呼叫方法。The OnItemCreated method is called before the created node item is bound to its data. 這可讓您在每次建立時,提供執行自訂常式的事件處理方法 SiteMapNodeItemThis allows you to provide an event-handling method that performs a custom routine whenever a SiteMapNodeItem is created.

引發事件會透過委派叫用此事件處理常式。Raising an event invokes the event handler through a delegate. 如需詳細資訊,請參閱 處理和引發事件For more information, see Handling and Raising Events.

OnItemCreated 方法也允許衍生類別處理事件,而不用附加委派。The OnItemCreated method also allows derived classes to handle the event without attaching a delegate. 這是在衍生類別中處理事件的慣用技巧。This is the preferred technique for handling the event in a derived class.

給繼承者的注意事項

當在衍生類別中覆寫 OnItemCreated(SiteMapNodeItemEventArgs) 時,請確定呼叫基底類別的 OnItemCreated(SiteMapNodeItemEventArgs) 方法,使已註冊的委派能接收到事件。When overriding OnItemCreated(SiteMapNodeItemEventArgs) in a derived class, be sure to call the base class's OnItemCreated(SiteMapNodeItemEventArgs) method so that registered delegates receive the event.

適用於

另請參閱