RadTreeView for ASP.NET

SiteMapDataSource Send comments on this topic.
See Also
Data Binding > SiteMapDataSource

Glossary Item Box

When data-bound to a SiteMapDataSource control, Telerik RadTreeView automatically creates treeview item hierarchy and maps the  title, url, and description fields of the sitemap to the Text, NavigateUrl, and ToolTip properties respectively.  

The SiteMapDataSource control allows you to define the structure of your site and bind any navigational control to it. You can have only one sitemap per site. By default the SiteMapDataSource control will use the default Site Navigation Provider. This provider allows you to define the structure of your site, separating the real page structure of the site on one hand, and the UI presentation on the other.
The default site navigation provider is XML-based. It should stay in the root level of your project and should be named web.sitemap. Using the Site Navigation API provided in ASP.NET v2.0, you can write your own Site Navigation Provider and expose the navigation information from any back-end system, if needed.

Setting the SiteMapDataSource control

Before you add the SiteMapDataSource control to your page, you need to add the Site Navigation Provider.

Use the solution explorer and the [Add New Item...] command. In the templates dialog, select Site Map.

When the web.sitemap file is created, Visual Studio generates the default initial code.

SiteMap Copy Code
<?xml version="1.0" encoding="utf-8" ?>

<
siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
 
<siteMapNode url="" title="" description="">
   
<siteMapNode url="" title="" description="" />
   
<siteMapNode url="" title="" description="" />
 
</siteMapNode>
</
siteMap>
        

Now you can go ahead and populate the Web.sitemap file. Here is an example:

SiteMap Copy Code
<?xml version="1.0" encoding="utf-8" ?><siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" > <siteMapNode url="http://www.telerik.com" title="Telerik" description="Telerik home page" MyCustomAttribute="root 1">   <siteMapNode url="http://www.telerik.com/radcontrols" title="Telerik RadControls for ASP.NET" description="Telerik RadControls for ASP.NET" MyCustomAttribute="item 1">     <siteMapNode url="http://www.telerik.com/radeditor" title="Telerik RadEditor" description="Telerik RadEditor control" MyCustomAttribute="item 1.1"/>   </siteMapNode>   <siteMapNode url="http://www.telerik.com/radnavigation" title="Telerik RadNavigation controls" description="Telerik RadNavigation controls" MyCustomAttribute="item 2"/> </siteMapNode></siteMap>         
Note, that the url must be unique for each node. 

To create a SiteMapDataSource instance, from the Toolbox, under Data, drag an instance of SiteMapDataSource to the design surface.

 

Binding the SiteMapDataSource to Telerik RadTreeView

From the property grid of the treeview, set the DataSourceID to the ID of the SiteMapDataSource control.

Each siteMapNode has three attributes which are mapped automatically to the respective properties: 

  • url - Maps to the NavigateUrl property.  The path can be either relative (using the ~/), or absolute.
    Avoid using backslashes ('\') for your URLs. Backslashes may cause problems with some browsers. Instead, we recommend using the slash character ('/').
  • title - Maps to the Text property.
  • description - Maps to the ToolTip property.

To set additional properties or overwrite already populated fields you can use the NodeBound event. The example below overwrites the Text property with the value of MyCustomAttribute of the sitemap node and sets the Value property to a concatenated string of [Text] + "_Value".

C# Copy Code
protected void RadTreeView1_NodeBound(object sender, Telerik.WebControls.RadTreeNodeEventArgs e){   //Access sitemap node properties such as title, url, description, etc.   e.NodeBound.Value = (string)DataBinder.Eval(e.NodeBound.DataItem, "title") + "_Value";    //Access custom attributes   SiteMapNode node1 = (SiteMapNode)e.NodeBound.DataItem;   e.NodeBound.Text = node1["MyCustomAttribute"];        }         
VB.NET Copy Code
Protected Sub RadTreeView1_NodeBound(ByVal sender As Object, ByVal e As Telerik.WebControls.RadTreeNodeEventArgs) Handles RadTreeView1.NodeBound 'Access sitemap node properties such as title, url, description, etc. e.NodeBound.Value = CStr(DataBinder.Eval(e.NodeBound.DataItem, "title")) + "_Value"
        'Access custom attributes: Dim node1 As SiteMapNode = DirectCast(e.NodeBound.DataItem, SiteMapNode) e.NodeBound.Text = node1("MyCustomAttribute")End Sub

 

You can view the code library project on databinding Telerik RadNavigational controls for a working demo application.

See Also