tree view - writing code when attached with sitemapdatasource

Posted by Gayathri under ASP.NET on 5/27/2011 | Points: 10 | Views : 2494 | Status : [Member] | Replies : 1
I have attached a sitemap datasource with treeview to display menu.
how to write code in the treeview .
for example
the menu in treeview is
as follows.
- View books
- login
- Registration.

Who ever wants to view book first need to login first they cannot directly go and click the view books.
so i need to write a code as follows.
if session[user] != null
--> then proceed (which means logged in)
go to login page.


Posted by: Ndebata on: 5/27/2011 [Member] Starter | Points: 25

Create a XML like this and save it in App_Data folder with name BookMenu.xml

<?xml version="1.0" encoding="utf-8" ?>
<Menu title="Books" NavUrl="Books.aspx" description="Books"/>
<Menu title="Login" NavUrl="Login.aspx" description="Login"/>
<Menu title="Registration" NavUrl="Registration.aspx" description="Registration"/>

Then in Page/MasterPage (where Treeview need to be added) add XmlDataSource and a TreeView

<asp:XmlDataSource ID="BookTree" runat="server" XPath="/*/*"></asp:XmlDataSource>
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="BookTree" SelectedNodeStyle-ForeColor="Black"
SelectedNodeStyle-Font-Bold="true" SelectedNodeStyle-Font-Size="13px">
<asp:TreeNodeBinding DataMember="Menu" NavigateUrlField="NavUrl" TextField="title"
ToolTipField="description" />

In the Code behind of Page/MasterPage OnLoad just read the xml and assign the datasource

protected override void OnLoad(EventArgs e)
XElement xml = XElement.Load(MapPath("~/App_Data/BookMenu.xml"));
var _data = from s in xml.Elements("Admin") select s;

BookTree.Data = Convert.ToString(_data.FirstOrDefault());


Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response