Home All Groups Group Topic Archive Search About

How to add DataSource to a TreeView programmatically

Author
29 Nov 2006 4:18 PM
anders.rask
Hi,

im creating a custom control (custom SPField in WSS v3) that renders a
TreeView control.

This works fine if i add TreeNodes manually to the nodes collection,
but what i really want to do is add a SiteMap provider as datasource
for the control.

I am doing this programmatically in CreateChildControls but so far
without much luck:

I get no errors, but the TreeView renders without any data in it.


Code (inside CreateChildControls of component):
           _siteMapDataSource = new SiteMapDataSource(); //defined
outside method
            _siteMapDataSource.SiteMapProvider =
"SPContentMapProvider";//taken from web.config
            _siteMapDataSource.StartFromCurrentNode = true;
            _siteMapDataSource.ShowStartingNode = false;

            _siteMapDataSource.DataBind();//not sure this is
necesarry..


            _spTreeView.DataSourceID = _siteMapDataSource.ID;
            _spTreeView.DataBind();

I tried with all the sitemap providers in web.config but none of them
seems to work...

any help or hint appreciated! :-)

thx
//AndersR

Author
30 Nov 2006 12:48 AM
MikeS
This appears to be all that is required.

            SiteMapDataSource smds = new SiteMapDataSource();
            smds.Provider = SiteMap.Providers["Default"]; // Name from
web.config
            TreeView1.DataSource = smds;
            TreeView1.DataBind();

ID is only used when you are using a datasource control.