SiteMapPath.OnItemCreated(SiteMapNodeItemEventArgs) 方法

定义

引发 SiteMapPath 控件的 ItemCreated 事件。

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)

参数

示例

下面的代码示例演示如何在 方法中创建 SiteMapNodeItemInitializeItem 后调用 OnItemCreated 方法。 此代码示例是为 SiteMapPath 类提供的一个更大示例的一部分。

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引发,该控件是一个代表 SiteMapNode的 Web 服务器控件,并将其关联到 。SiteMapNodeOnItemCreated 创建的节点项绑定到其数据之前调用 方法。 这样,就可以提供一个事件处理方法,该方法在创建 SiteMapNodeItem 时执行自定义例程。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnItemCreated 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnItemCreated(SiteMapNodeItemEventArgs) 时,一定要调用基类的 OnItemCreated(SiteMapNodeItemEventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅