Part 155 Binding asp net menu control to an xml file using xmldatasource control
Text version of the video
Slides
All ASP .NET Text Articles
All ASP .NET Slides
ASP.NET Playlist
All Dot Net and SQL Server Tutorials in English
All Dot Net and SQL Server Tutorials in Arabic
In this video, we will discuss binding asp.net menu control to an xml file using xmldatasource control. This is continuation to Part 154, please watch Part 154 before proceeding.
1. Add an XML file and name it MenuData.xml. Include the menu item xml.
2. Drag and drop an XmlDataSource control on the webform. Set XPath and DataFile attributes. Notice that DataFile attribute points to the XML file that we added in Step 1.
3. Drag and drop a menu control and set DataSourceID attribute to the xmldatasource control we created in step 2. Also, set DataBindings.
4. To set the styles for the selected menu item, copy and paste the following code in the code-behind file.
private void Check(MenuItem item)
{
if (item.NavigateUrl.Equals(Request.AppRelativeCurrentExecutionFilePath,
StringComparison.InvariantCultureIgnoreCase))
{
item.Selected = true;
}
else if (item.ChildItems.Count ] 0)
{
foreach (MenuItem menuItem in item.ChildItems)
{
Check(menuItem);
}
}
}
protected void Menu1_PreRender(object sender, EventArgs e)
{
foreach (MenuItem item in Menu1.Items)
{
Check(item);
}
}