Export (0) Print
Expand All

SiteMapNodeCollection.Add Method

Adds a single SiteMapNode object to the collection.

Namespace:  System.Web
Assembly:  System.Web (in System.Web.dll)

public virtual int Add(
	SiteMapNode value
)

Parameters

value
Type: System.Web.SiteMapNode

The SiteMapNode to add to the SiteMapNodeCollection.

Return Value

Type: System.Int32
The index of the InnerList where the SiteMapNode was inserted.

ExceptionCondition
ArgumentNullException

value is null.

NotSupportedException

The SiteMapNodeCollection is read-only.

You cannot add a SiteMapNode object to a read-only or fixed-size SiteMapNodeCollection collection. You can test whether a SiteMapNodeCollection is read-only by checking the IsReadOnly property.

This section contains two code examples. The first code example demonstrates how to create a new SiteMapNodeCollection collection and add elements to it. The second code example demonstrates how to add elements to a custom site map provider.

The following code example demonstrates how to use the SiteMapNodeCollection constructor to create a new SiteMapNodeCollection, and then add elements to it with the Add method.

// The LoadSiteMapData() method loads site navigation 
// data from persistent storage into a DataTable.
DataTable siteMap = LoadSiteMapData();

// Create a SiteMapNodeCollection.
SiteMapNodeCollection nodes = new SiteMapNodeCollection();

// Create a SiteMapNode and add it to the collection.
SiteMapNode tempNode;
DataRow row;
int index = 0;

while (index < siteMap.Rows.Count)
{

    row = siteMap.Rows[index];

    // Create a node based on the data in the DataRow.
    tempNode = new SiteMapNode(SiteMap.Provider,
                                row["Key"].ToString(),
                                row["Url"].ToString());

    // Add the node to the collection.
    nodes.Add(tempNode);
    ++index;
}

The following code example demonstrates a custom site map provider implementation and shows how to use the SiteMapNodeCollection constructor to create a new SiteMapNodeCollection and add elements to the SiteMapNodeCollection with the Add method.

This code example is part of a larger code example provided for the SiteMapProvider class.

// Implement the GetChildNodes method. 
public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
{
  SiteMapNodeCollection children = new SiteMapNodeCollection();
  // Iterate through the ArrayList and find all nodes that have the specified node as a parent. 
  lock (this)
  {
    for (int i = 0; i < childParentRelationship.Count; i++)
    {

      string nodeUrl = ((DictionaryEntry)childParentRelationship[i]).Key as string;

      SiteMapNode parent = GetNode(childParentRelationship, nodeUrl);

      if (parent != null && node.Url == parent.Url)
      {
        // The SiteMapNode with the Url that corresponds to nodeUrl 
        // is a child of the specified node. Get the SiteMapNode for 
        // the nodeUrl.
        SiteMapNode child = FindSiteMapNode(nodeUrl);
        if (child != null)
        {
          children.Add(child as SiteMapNode);
        }
        else
        {
          throw new Exception("ArrayLists not in sync.");
        }
      }
    }
  }
  return children;
}
protected override SiteMapNode GetRootNodeCore()
{
  return RootNode;
}
// Implement the GetParentNode method. 
public override SiteMapNode GetParentNode(SiteMapNode node)
{
  // Check the childParentRelationship table and find the parent of the current node. 
  // If there is no parent, the current node is the RootNode.
  SiteMapNode parent = null;
  lock (this)
  {
    // Get the Value of the node in childParentRelationship
    parent = GetNode(childParentRelationship, node.Url);
  }
  return parent;
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft