Editare

Partajați prin


SiteMapNode Constructors

Definition

Initializes a new instance of the SiteMapNode class and associates it with the specified SiteMapProvider object.

Overloads

SiteMapNode(SiteMapProvider, String)

Initializes a new instance of the SiteMapNode class, using the specified key to identify the page that the node represents and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title and description, and the site map provider that manages the node.

SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String)

Initializes a new instance of the SiteMapNode class using the specified site map provider that manages the node, URL, title, description, roles, additional attributes, and explicit and implicit resource keys for localization.

SiteMapNode(SiteMapProvider, String)

Initializes a new instance of the SiteMapNode class, using the specified key to identify the page that the node represents and the site map provider that manages the node.

public:
 SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key);
public SiteMapNode (System.Web.SiteMapProvider provider, string key);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String)

Parameters

provider
SiteMapProvider

The SiteMapProvider with which the node is associated.

key
String

A provider-specific lookup key.

Exceptions

SiteMapProvider is null.

-or-

key is null.

Remarks

The XmlSiteMapProvider class, which is the default SiteMapProvider provider implementation for ASP.NET, uses the SiteMapNode.Url property as a lookup key, if one is provided for the node (if a URL is not provided, a tracking identifier is generated for the node). Therefore, any SiteMapNode control that provides a URL and is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

Applies to

SiteMapNode(SiteMapProvider, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, and the site map provider that manages the node.

public:
 SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url);
public SiteMapNode (System.Web.SiteMapProvider provider, string key, string url);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String)

Parameters

provider
SiteMapProvider

The SiteMapProvider with which the node is associated.

key
String

A provider-specific lookup key.

url
String

The URL of the page that the node represents within the site.

Exceptions

SiteMapProvider is null.

-or-

key is null.

Examples

The following code example demonstrates how to use the SiteMapNodeCollection constructor to create a new SiteMapNodeCollection collection, 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 LoadSiteMapData() Function loads site navigation
' data from persistent storage into a DataTable.

Dim siteMapData As DataTable
siteMapData = LoadSiteMapData()

' Create a SiteMapNodeCollection.
Dim nodes As New SiteMapNodeCollection()

' Create a SiteMapNode and add it to the collection.
Dim tempNode As SiteMapNode
Dim row As DataRow
Dim index As Integer
index = 0

While (index < siteMapData.Rows.Count)

    row = siteMapData.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 = index + 1
End While

Remarks

The XmlSiteMapProvider class, which is the default SiteMapProvider provider implementation for ASP.NET, uses the SiteMapNode.Url property as a lookup key, if one is provided for the node (if a URL is not provided, a tracking identifier is generated for the node). Therefore, any SiteMapNode object that provides a URL and is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

Applies to

SiteMapNode(SiteMapProvider, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title, and the site map provider that manages the node.

public:
 SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title);
public SiteMapNode (System.Web.SiteMapProvider provider, string key, string url, string title);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String)

Parameters

provider
SiteMapProvider

The SiteMapProvider with which the node is associated.

key
String

A provider-specific lookup key.

url
String

The URL of the page that the node represents within the site.

title
String

A label for the node, often displayed by navigation controls.

Exceptions

SiteMapProvider is null.

-or-

key is null.

Examples

The following code example demonstrates how to use the SiteMapNode constructor to create a new instance of the SiteMapNode class. The node is initialized with values from a Microsoft Access database row.

This code example is part of a larger example provided for the BuildSiteMap method.

OleDbCommand^ rootNodeCommand = gcnew OleDbCommand
   ("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection);
OleDbDataReader^ rootNodeReader = rootNodeCommand->ExecuteReader();
if ( rootNodeReader->HasRows )
{
   rootNodeReader->Read();
   rootNodeId = rootNodeReader->GetInt32( 0 );
   
   // Create a SiteMapNode that references the current StaticSiteMapProvider.
   rootNode = gcnew SiteMapNode(this, rootNodeId.ToString(), 
      rootNodeReader->GetString( 1 ),rootNodeReader->GetString( 2 ));
}
else
   return nullptr;
rootNodeReader->Close();

OleDbCommand rootNodeCommand =
    new OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL",
                     accessConnection);
OleDbDataReader rootNodeReader = rootNodeCommand.ExecuteReader();

if(rootNodeReader.HasRows) {
    rootNodeReader.Read();
    rootNodeId = rootNodeReader.GetInt32(0);
    // Create a SiteMapNode that references the current StaticSiteMapProvider.
    rootNode   = new SiteMapNode(this,
                                 rootNodeId.ToString(),
                                 rootNodeReader.GetString(1),
                                 rootNodeReader.GetString(2));
}
else
{
    return null;
}

rootNodeReader.Close();
Dim rootNodeCommand As New OleDbCommand("SELECT nodeid, url, name FROM SiteMap WHERE parentnodeid IS NULL", accessConnection)
Dim rootNodeReader As OleDbDataReader = rootNodeCommand.ExecuteReader()

If rootNodeReader.HasRows Then
    rootNodeReader.Read()
    rootNodeId = rootNodeReader.GetInt32(0)
    ' Create a SiteMapNode that references the current StaticSiteMapProvider.
    aRootNode = New SiteMapNode(Me, rootNodeId.ToString(), rootNodeReader.GetString(1), rootNodeReader.GetString(2))
Else
    Return Nothing
End If
rootNodeReader.Close()

Remarks

The XmlSiteMapProvider class, which is the default SiteMapProvider provider implementation for ASP.NET, uses the SiteMapNode.Url property as a lookup key, if one is provided for the node (if a URL is not provided, a tracking identifier is generated for the node). Therefore, any SiteMapNode object that provides a URL and is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

If no title is provided, calls to the Title property return the String.Empty field.

Applies to

SiteMapNode(SiteMapProvider, String, String, String, String)

Initializes a new instance of the SiteMapNode class using the specified URL, a key to identify the page that the node represents, a title and description, and the site map provider that manages the node.

public:
 SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title, System::String ^ description);
public SiteMapNode (System.Web.SiteMapProvider provider, string key, string url, string title, string description);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String, description As String)

Parameters

provider
SiteMapProvider

The SiteMapProvider with which the node is associated.

key
String

A provider-specific lookup key.

url
String

The URL of the page that the node represents within the site.

title
String

A label for the node, often displayed by navigation controls.

description
String

A description of the page that the node represents.

Exceptions

SiteMapProvider is null.

-or-

key is null.

Examples

The following code example demonstrates how to use the SiteMapNode constructor to create a SiteMapNode object by parsing data from a simple text file to build a site map in memory.

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

protected virtual void LoadSiteMapFromStore()
{
  string pathToOpen;

  lock (this)
  {
    // If a root node exists, LoadSiteMapFromStore has already
    // been called, and the method can return.
    if (rootNode != null)
    {
      return;
    }
    else
    {
      pathToOpen = HttpContext.Current.Server.MapPath("~" + "\\" + sourceFilename);

      if (File.Exists(pathToOpen))
      {
        // Open the file to read from.
        using (StreamReader sr = File.OpenText(pathToOpen))
        {

          // Clear the state of the collections and rootNode
          rootNode = null;
          siteMapNodes.Clear();
          childParentRelationship.Clear();

          // Parse the file and build the site map
          string s = "";
          string[] nodeValues = null;
          SiteMapNode temp = null;

          while ((s = sr.ReadLine()) != null)
          {

            // Build the various SiteMapNode objects and add
            // them to the ArrayList collections. The format used
            // is: URL,TITLE,DESCRIPTION,PARENTURL

            nodeValues = s.Split(',');

            temp = new SiteMapNode(this,
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[0],
                nodeValues[1],
                nodeValues[2]);

            // Is this a root node yet?
            if (null == rootNode &&
                string.IsNullOrEmpty(nodeValues[3]))
            {
              rootNode = temp;
            }

          // If not the root node, add the node to the various collections.
            else
            {
              siteMapNodes.Add(new DictionaryEntry(temp.Url, temp));
              // The parent node has already been added to the collection.
              SiteMapNode parentNode =
                       FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath + "/" + nodeValues[3]);
              if (parentNode != null)
              {
                childParentRelationship.Add(new DictionaryEntry(temp.Url, parentNode));
              }
              else
              {
                throw new Exception("Parent node not found for current node.");
              }
            }
          }
        }
      }
      else
      {
        throw new Exception("File not found");
      }
    }
  }
  return;
}
  Protected Overridable Sub LoadSiteMapFromStore()
    Dim pathToOpen As String
    SyncLock Me
      ' If a root node exists, LoadSiteMapFromStore has already
      ' been called, and the method can return.
      If Not (aRootNode Is Nothing) Then
        Return
      Else
        pathToOpen = HttpContext.Current.Server.MapPath("~" & "\\" & sourceFilename)
        If File.Exists(pathToOpen) Then
          ' Open the file to read from.
          Dim sr As StreamReader = File.OpenText(pathToOpen)
          Try

            ' Clear the state of the collections and aRootNode
            aRootNode = Nothing
            siteMapNodes.Clear()
            childParentRelationship.Clear()

            ' Parse the file and build the site map
            Dim s As String = ""
            Dim nodeValues As String() = Nothing
            Dim temp As SiteMapNode = Nothing

            Do
              s = sr.ReadLine()

              If Not s Is Nothing Then
                ' Build the various SiteMapNode objects and add
                ' them to the ArrayList collections. The format used
                ' is: URL,TITLE,DESCRIPTION,PARENTURL
                nodeValues = s.Split(","c)

                temp = New SiteMapNode(Me, _
                    HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                    HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(0), _
                    nodeValues(1), _
                    nodeValues(2))

                ' Is this a root node yet?
                If aRootNode Is Nothing AndAlso _
                  (nodeValues(3) Is Nothing OrElse _
                   nodeValues(3) = String.Empty) Then
                  aRootNode = temp

                  ' If not the root node, add the node to the various collections.
                Else

                  siteMapNodes.Add(New DictionaryEntry(temp.Url, temp))

                  ' The parent node has already been added to the collection.
                  Dim parentNode As SiteMapNode = _
                      FindSiteMapNode(HttpRuntime.AppDomainAppVirtualPath & "/" & nodeValues(3))

                  If Not (parentNode Is Nothing) Then
                    childParentRelationship.Add(New DictionaryEntry(temp.Url, parentNode))
                  Else
                    Throw New Exception("Parent node not found for current node.")
                  End If
                End If
              End If
            Loop Until s Is Nothing
          Finally
            sr.Close()
          End Try
        Else
          Throw New Exception("File not found")
        End If
      End If
    End SyncLock
    Return
  End Sub
End Class

Remarks

The XmlSiteMapProvider class, which is the default SiteMapProvider provider implementation for ASP.NET, uses the SiteMapNode.Url property as a lookup key, if one is provided for the node (if a URL is not provided, a tracking identifier is generated for the node). Therefore, any SiteMapNode object that provides a URL and is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

If no title or description is provided, calls to the Title or Description properties return an String.Empty field.

See also

Applies to

SiteMapNode(SiteMapProvider, String, String, String, String, IList, NameValueCollection, NameValueCollection, String)

Initializes a new instance of the SiteMapNode class using the specified site map provider that manages the node, URL, title, description, roles, additional attributes, and explicit and implicit resource keys for localization.

public:
 SiteMapNode(System::Web::SiteMapProvider ^ provider, System::String ^ key, System::String ^ url, System::String ^ title, System::String ^ description, System::Collections::IList ^ roles, System::Collections::Specialized::NameValueCollection ^ attributes, System::Collections::Specialized::NameValueCollection ^ explicitResourceKeys, System::String ^ implicitResourceKey);
public SiteMapNode (System.Web.SiteMapProvider provider, string key, string url, string title, string description, System.Collections.IList roles, System.Collections.Specialized.NameValueCollection attributes, System.Collections.Specialized.NameValueCollection explicitResourceKeys, string implicitResourceKey);
new System.Web.SiteMapNode : System.Web.SiteMapProvider * string * string * string * string * System.Collections.IList * System.Collections.Specialized.NameValueCollection * System.Collections.Specialized.NameValueCollection * string -> System.Web.SiteMapNode
Public Sub New (provider As SiteMapProvider, key As String, url As String, title As String, description As String, roles As IList, attributes As NameValueCollection, explicitResourceKeys As NameValueCollection, implicitResourceKey As String)

Parameters

provider
SiteMapProvider

The SiteMapProvider with which the node is associated.

key
String

A provider-specific lookup key.

url
String

The URL of the page that the node represents within the site.

title
String

A label for the node, often displayed by navigation controls.

description
String

A description of the page that the node represents.

roles
IList

An IList of roles that are allowed to view the page represented by the SiteMapNode.

attributes
NameValueCollection

A NameValueCollection of additional attributes used to initialize the SiteMapNode.

explicitResourceKeys
NameValueCollection

A NameValueCollection of explicit resource keys used for localization.

implicitResourceKey
String

An implicit resource key used for localization.

Exceptions

SiteMapProvider is null.

-or-

key is null.

Remarks

The XmlSiteMapProvider class, which is the default SiteMapProvider provider implementation for ASP.NET, uses the SiteMapNode.Url property as a lookup key, if one is provided for the node (if a URL is not provided, a tracking identifier is generated for the node). Therefore, any SiteMapNode object that provides a URL and is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

The NameValueCollection collection of attributes that the SiteMapNode is created with is available through the Attributes property, and makes it easy to apply additional attributes to a site map node and extend its capabilities without deriving a custom class to do so.

Supply an IList collection of roles to provide a set of roles that can view the node when the SecurityTrimmingEnabled property returns true. For more information, see SecurityTrimmingEnabled.

The XmlSiteMapProvider provider uses the SiteMapNode.Url property as a lookup key. Therefore, any SiteMapNode that is used by the XmlSiteMapProvider must have a unique URL within the scope of the provider.

If no title or description is provided, calls to the Title or Description properties return an String.Empty field.

To programmatically specify resources for localization, either set the value of implicitResourceKey to a unique name that will be used to identify localized resources for the node or set explicitResourceKeys to a NameValueCollection collection of name/value pairs where name is the node property or custom attribute to localize and value contains localization values for the node property or custom attribute. The localized values can then be set in the appropriate .resx files. For more information about how to localize the Title, Description, and any custom properties of a SiteMapNode object, see How to: Localize Site-Map Data. For the syntax requirements of the explicitResourceKeys collection, see NameValueCollection.

See also

Applies to