Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Create a Menu in ASP.NET 2.0
Author: Sonu Kapoor
Published: 5/10/2005 8:23 PM
Category: XML
Summary: This example will show you how you can create a menu based on the Menu and XmlDataSouce control.

 
 

[ASP.NET 2.0] Building a Menu with the Menu and XmlDataSource control.

This example will show you how you can easily create an menu with the Menu control. The menu data will be simply stored in a XML file. We are simply using a XmlDataSource control which will load the XML automatically via the DataFile property. The first thing you need to do is to drag/drop one menu control into the designer and one XmlDataSouce control. Define a XML file which looks like the below one:

<?xml version="1.0" encoding="utf-8" ?>
<
menuitems>
  <
userprofile>
    <
menuitem title="Home" path="~/default.aspx" />
    <
menuitem title="Blog" path="~/blog/default.aspx" />
    <
menuitem title="Articles" path="~/articles.aspx" />
    <
menuitem title="Contact" path="~/contact.aspx" />
  </
userprofile>
  <
adminprofile>
    <
menuitem title="Edit Article" path="~/admin/editarticle.aspx" />
    <
menuitem title="Edit User" path="~/admin/edituser.aspx" />
    <
menuitem title="Stats" path="~/admin/stats.aspx" />
  </
adminprofile>
</
menuitems>

Now go back to the designer of your aspx page and define your Menu control and XmlDataSource controls like this:

<asp:Menu ID="Menu1" runat="server" BackColor="#B5C7DE" DataSourceID="XmlDataSource1" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px">
  <StaticSelectedStyle BackColor="#507CD1" />
 
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
 
<DynamicHoverStyle BackColor="#284E98" Font-Bold="False" ForeColor="White" />
 
<DynamicMenuStyle BackColor="#B5C7DE" />
 
<DynamicSelectedStyle BackColor="#507CD1" />
 
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
 
<StaticHoverStyle BackColor="#284E98" Font-Bold="False" ForeColor="White" />
 
<DataBindings>
   
<asp:MenuItemBinding DataMember="menuitem" NavigateUrlField="path" TextField="title" ValueField="title" />
  
</DataBindings>
</asp:Menu>

<asp:XmlDataSource ID="XmlDataSource1"  runat="server" DataFile="~/xmldatasource.xml" xpath="/menuitems/userprofile/menuitem"></asp:XmlDataSource>

<p><strong><em>Admin Section</em></strong></p><asp:Menu ID="Menu2" runat="server" BackColor="#B5C7DE" DataSourceID="XmlDataSource2" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px">
 
<StaticSelectedStyle BackColor="#507CD1" />
 
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
 
<DynamicHoverStyle BackColor="#284E98" Font-Bold="False" ForeColor="White" />
 
<DynamicMenuStyle BackColor="#B5C7DE" />
 
<DynamicSelectedStyle BackColor="#507CD1" />
 
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
 
<StaticHoverStyle BackColor="#284E98" Font-Bold="False" ForeColor="White" />
 
<DataBindings>
 
<asp:MenuItemBinding DataMember="menuitem" NavigateUrlField="path" TextField="title" ValueField="title" />
 
</DataBindings>
</asp:Menu>

<asp:XmlDataSource ID="XmlDataSource2"  runat="server" DataFile="~/xmldatasource.xml" xpath="/menuitems/adminprofile/menuitem">
</asp:XmlDataSource>

Thats all you need to create a menu based on XML. As you can see we didn't write one line of code. All properties can be set via the designer. Thats one of the major changes in asp.net 2.0.


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
online backup
Color Laser Printer
designer sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139