SiteMapPath.OnItemCreated(SiteMapNodeItemEventArgs) Method

Definition

引发 SiteMapPath 控件的 ItemCreated 事件。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)

Parameters

e
SiteMapNodeItemEventArgs

包含事件数据的 SiteMapNodeItemEventArgsA SiteMapNodeItemEventArgs that contains event data.

Examples

下面的代码示例演示了如何在 InitializeItem 方法中创建 SiteMapNodeItem 之后调用 OnItemCreated 方法。The following code example demonstrates how to call the OnItemCreated method after creating a SiteMapNodeItem within the InitializeItem method. 此代码示例是为 SiteMapPath 类提供的更大示例的一部分。This 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

Remarks

ItemCreated 事件在 SiteMapPath 控件创建 SiteMapNodeItem(表示 SiteMapNode的 Web 服务器控件)后引发,并将其与 SiteMapNode关联。The 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. 这使你可以提供一种事件处理方法,该方法可在创建 SiteMapNodeItem 时执行自定义例程。This 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.

Notes to Inheritors

在派生类中重写 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.

Applies to

See also