XmlDataSource – Treeview databind based on Parent – Child xml file


Do you need to implement an ASP:TreeView in you website based on a xml file with a nice nested Parent – Child structure? I think that everyone already wrote, at least one time in his life, a recursive function to accomplish this but this can be better 🙂

I found a very good post on this matter: http://www.4guysfromrolla.com/articles/042308-1.aspx

I wasn’t aware of this solution, so I learned something! 🙂

ASPX Page:

<asp:XmlDataSourceID=”xmlData”runat=”server”/>
<asp:TreeViewID=”trvGeoMap”runat=”server”OnSelectedNodeChanged=”trvGeoMap_OnSelectedNodeChanged”AutoGenerateDataBindings=”false”DataSourceID=”xmlData”EnableViewState=”true”>
<DataBindings>
<asp:TreeNodeBindingDataMember=”GeoMap”Text=”Detail”/>
<asp:TreeNodeBindingDataMember=”Node”FormatString=”{0}”TextField=”title”ImageUrl=”/_layouts/images/EMMTerm.png”/>
</DataBindings>
</asp:TreeView>

Xml-File:

<GeoMap>
<Node title=”France”>
<Node title=”Paris”>
<FieldRef title=”Population” value=”2,211,297″/>
</Node>
</Node>
<Node title=”Belgium”>
<Node title=”Antwerp”>
<FieldRef title=”Population” value=”507,050″/>
</Node>
<Node title=”Brussel”>
<FieldRef title=”Population” value=”1,800,000″/>
</Node>
</Node>
</GeoMap>

Code-Behind (the xml-file is located in SharePoint ;-))

using (SPWeb spWeb = SPContext.Current.Site.OpenWeb(“KB”))
{
SPFile spFile = spWeb.GetFile(“/kb/Templates/DocDetail_TreeFile.xml”);
StreamReader reader = new StreamReader(spFile.OpenBinaryStream());
xmlData.Data = reader.ReadToEnd();
trvFiches.DataBind();
}

Advertisements
This entry was posted in Geen categorie and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s