How to add DataSource to a TreeView programmatically

A

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
 
M

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top