CodeSnip: Building a Site Map with ASP.NET 2.0
page 1 of 1
Published: 19 Jul 2005
Unedited - Community Contributed
Abstract
With the evolution of ASP.NET 2.0, creating a site map for your website is easier than ever. In this article, Anand Narayanaswamy provides a step-by-step demonstration of using the new SiteMapDataSource and TreeView controls.
by Anand Narayanaswamy
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 68438/ 65

ASP.NET 2.0 ships with many new controls to reduce development time. In this article, you will learn how to build a site map for your web site. First, you will require a file with the contents required to create a site map. This file is in XML format. We will employ Visual Studio 2005 in order to create this file.

Before creating a Site Map file, you should create a new web project. Within Visual Studio 2005, click on the File menu and select New Web Site. Select the ASP.NET Web Site template and provide a meaningful name for the project, as shown in figure 1.

Figure 1


When you click the OK button in the New Web Site dialog, Visual Studio 2005 creates the files required for successfully running the project. Let us now create a Site Map file. Click the Website menu and select the Add New Item menu option. Select the Site Map entry from the Visual Studio installed templates section, and leave the Name field as it is, as shown in figure 2. We will use the default file name for this article.

Figure 2


Visual Studio automatically creates the required initial code as shown in listing 1. Copy the code given in listing 2, paste this code into the Site Map file you created, and save the file.

Listing 1

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode url="" title=""  description="">
        <siteMapNode url="" title=""  description="" />
        <siteMapNode url="" title=""  description="" />
    </siteMapNode>
</siteMap>

Listing 2

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
    <siteMapNode title="Welcome to Page 1"  description="Page 1">
        <siteMapNode url=<a href="http://www.microsoft.com">http://www.microsoft.com</a
               title="Welcome to Microsoft" description="MICROSOFT" />
        <siteMapNode url=<a href="http://www.sun.com">http://www.sun.com</a
               title="Welcome to Sun" description="SUN" />
        <siteMapNode url=<a href="http://www.oracle.com">http://www.oracle.com</a
               title="Welcome to Oracle" description="SUN" />
        <siteMapNode title="Welcome to Page 2"  description="Page 2">
            <siteMapNode url=<a href="http://msdn.microsoft.com">http://msdn.microsoft.com</a
                   title="Welcome to MSDN" description="MSDN" />
            <siteMapNode url=<a href="http://www.asp.net">http://www.asp.net</a
                   title="Welcome to ASP.NET" description="ASP.NET" />
            <siteMapNode url=<a href="http://www.aspalliance.com">http://www.aspalliance.com</a
                   title="Welcome to ASP Alliance" description="ASP.NET" />
        </siteMapNode>
    </siteMapNode>
</siteMap>

Open the Default.aspx file and select its Design view. From the Data section of the Toolbox, add a SiteMapDataSource control to the form. From the Navigation section of the Toolbox, add a TreeView control to the form. For each control, and leave the ID property as it is, as we will use the default values in this article.

Select the TreeView control and use the Properties window to set its DataSourceID property to the SiteMapDataSource control, as shown in figure 3.

Figure 3


Once you select the SiteMapDataSource as the DataSource for the TreeView control, you will be able to view immediately the Site Map's contents inside the TreeView control, as shown in figure 4. Save and run the application.

Figure 4


The code for the Default.aspx file will look similar to the code shown in listing 3.

Listing 3

<form id="form1" runat="server">
        <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
        <br />
        <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource1" Height="200px">
        </asp:TreeView>

The final output will look similar to the page shown in figure 5.

Figure 5


That’s it. You have successfully created a sitemap for your website using a TreeView control.

You can also bind the above Site Map file to a Menu control (which ships with ASP.NET 2.0) in one easy step. From the Navigation section of the Toolbox, drag and drop a Menu control on to the form and set its DataSourceID property to the SiteMapDataSource control. Run the application and you will be able to view the newly created menu as shown in figures 6 and 7. You can play with various properties of the Menu control, such as its appearance and behavior, using the Properties window in Visual Studio 2005.

Figure 6: Menu control in its inactive state

 

Figure 7: Menu control while hovering over the first menu item


In this article, you have learned how to build a simple site map for your website with two powerful controls that ship with the .NET Framework 2.0. You should now understand how easy it is to develop web applications using ASP.NET 2.0.

Happy Coding…



User Comments

Title: thanks alot   
Name: vani
Date: 3/4/2012 2:31:39 AM
Comment:
thanks it helped me alot.
Title: Great   
Name: Kourosh
Date: 12/29/2010 4:10:06 PM
Comment:
This article was so helpful to me.
Thanks
Title: Thanks   
Name: Samujeet
Date: 11/4/2010 7:49:21 AM
Comment:
Very Good Example NICE :-)
Title: thanks   
Name: monika
Date: 7/28/2010 1:56:26 AM
Comment:
wonderful!
Title: Sitemap   
Name: anony
Date: 5/10/2010 12:37:32 PM
Comment:
Cheers dude!
Title: Sitemap   
Name: Sonu Tyagi
Date: 5/10/2010 7:59:46 AM
Comment:
Oh.its really nice Stuff.thanks Buddy....
Title: sitemap   
Name: iliyash ahamad siddiqui
Date: 5/5/2010 11:58:49 AM
Comment:
very good stuff about sitemap
Title: thanks bro   
Name: lalit kumar
Date: 4/22/2010 10:03:08 PM
Comment:
u r very gud wish u will help me future
Title: SiteMap   
Name: karan dahda
Date: 4/18/2010 5:16:43 AM
Comment:
Great article,
with simplicity it explained alot about sitmaps,good.
Title: Excellent article   
Name: Avinash Kumar
Date: 3/23/2010 1:20:11 AM
Comment:
This article helps me a lot in understanding of SiteMap.
Title: sitemap provider   
Name: nirav
Date: 3/12/2010 12:46:39 AM
Comment:
how to add sitemap provider to web config file.
thanks
Title: Multiple Sitemap files   
Name: RK
Date: 3/11/2010 11:47:12 AM
Comment:
Hi,
Can we have "Multiple Sitemap Files" linked to Multiple Menu Controls in a single web page!
Title: Programmer Analyst   
Name: Priya
Date: 2/9/2010 4:10:02 PM
Comment:
excellent!
Title: SiteMapNode Information Required   
Name: Anshul Shrivastava
Date: 1/13/2010 9:13:10 AM
Comment:
It is very Informational link.But pl provide the information for how to change the size of siteMapNode in Sitemap.
Title: kkp   
Name: khushi rocks
Date: 11/27/2009 3:17:40 AM
Comment:
thank you i hope that it will work in my project
Title: gd   
Name: tiru
Date: 10/24/2009 6:11:31 AM
Comment:
its amazing
Title: Anand Rocks. . . .   
Name: Gopal
Date: 8/3/2009 7:16:31 AM
Comment:
Very useful article for easy rendering and practice for beginners.. Please continue with other controls and techniques...
Title: Good for Beginners   
Name: Yogi
Date: 7/30/2009 2:54:35 AM
Comment:
It is really good with the step by step approach
Title: Good for beginners   
Name: Sai
Date: 7/7/2009 2:55:57 AM
Comment:
Good for beginners.. Thanks
Title: Thanks   
Name: ducviet_195 at yahoo dot com dot vn
Date: 4/11/2009 12:38:36 PM
Comment:
good for my beginning !
Title: thanks   
Name: lephu55
Date: 4/11/2009 12:34:50 PM
Comment:
very good for amaters, goodluck in your life.
Title: thanks from dear   
Name: kamlesh
Date: 4/6/2009 9:15:48 AM
Comment:
I m begginer this boost for me for using these controls
Title: Cool; but..   
Name: Mark
Date: 1/15/2009 1:01:42 AM
Comment:
What about an article generating XML sitemaps with automatically from a database?
Title: very good for beginners   
Name: Ahmed Ali Khan
Date: 10/23/2008 3:48:15 AM
Comment:
very easy to understand by every one from start to end
Title: Thanks   
Name: sailaja
Date: 9/26/2008 5:47:00 PM
Comment:
very smooth and clearly quoted
Title: thanks   
Name: hema
Date: 9/12/2008 2:14:10 AM
Comment:
g8 job.............plz add more such explanations of asp.net
Title: Thank u.......   
Name: giri
Date: 9/12/2008 1:16:49 AM
Comment:
Thank u.....
Title: Keep it up great job   
Name: Aspcsharper
Date: 9/4/2008 12:05:02 AM
Comment:
Keep it up great job
Title: Nice   
Name: Gaurav
Date: 8/30/2008 11:15:39 AM
Comment:
Thanx
Title: Good Work   
Name: Kiran
Date: 7/15/2008 4:12:53 AM
Comment:
Hey dude nice work
Title: Great !!   
Name: Kuldeep
Date: 6/27/2008 8:06:07 AM
Comment:
This is really Good for beginners.

thanks a lot buddy.
Title: thanks for better understanding   
Name: Parthiv dave
Date: 6/17/2008 2:21:34 AM
Comment:
thanks for giving good description about how to use sitemap...
Title: creating sitemap in asp.net   
Name: Karthikeyan.S
Date: 5/30/2008 6:39:13 AM
Comment:
By using this site i have cleared the doubts how to create sitemap and all......Still I have to learn so many things from u.........thanks....
Title: Sitemap   
Name: Mak
Date: 5/23/2008 11:24:06 PM
Comment:
Its a very basic code...u not shld go into the details ...like images and css etc
Title: Good!   
Name: Elmira
Date: 4/7/2008 6:00:04 AM
Comment:
Simple and clear - great place to start!
Title: FeedBack   
Name: UmangPAtel
Date: 3/8/2008 10:38:33 AM
Comment:
NO i don't Like This Code...
U Specified Listing 2 But Where We Paste Code..
If i am paste the code then it's Give Errors...
Please Read Carefully
Title: Good   
Name: UmangPatel
Date: 3/7/2008 11:51:23 AM
Comment:
Ya It's Good For Us ...
Thank's FOr Giving Informataion
Title: WOW !!!!!!! !!!!!   
Name: dhaval
Date: 2/26/2008 9:30:51 AM
Comment:
it's nice get ...........
Title: Thank you very much   
Name: srikanth
Date: 2/23/2008 3:59:55 AM
Comment:
Awesome information.
Title: Thanks   
Name: Anitha
Date: 2/4/2008 10:44:55 AM
Comment:
Explanation step by step is good, it's easy to follow for beginners. Please will u tell me how to use sitemappath control.
Title: Thanks Anand Narayanaswamy   
Name: Pooja
Date: 1/31/2008 6:29:59 AM
Comment:
Thanks & a lot
Title: sitemap   
Name: thirumurugan
Date: 1/22/2008 7:22:11 AM
Comment:
its factastic ,nice
Title: Building Sitemap   
Name: Sanjay David
Date: 12/13/2007 6:15:29 AM
Comment:
It really good to give lucid explanation Thanks Narayana swamy .

Regards

R.Sanjay David B.E ,MCAD.NET
Title: looking for some advanced articles   
Name: Monishankar
Date: 10/22/2007 5:01:52 AM
Comment:
Its good but too basic.Expecting some advanced articles.
Title: easy   
Name: madhes
Date: 10/13/2007 10:22:40 AM
Comment:
easy to understand, thank u for given us,
Title: Easy to Understand   
Name: Pranav
Date: 9/6/2007 2:09:58 AM
Comment:
thanx, the article above is very good and information given might prove to be very useful for the programmers like me and many others. Thank u for the information.
Title: Great!!   
Name: Aakash
Date: 9/3/2007 12:11:47 PM
Comment:
It's mind blowing, great
Title: Too Much   
Name: Murthy
Date: 8/3/2007 3:51:44 AM
Comment:
It's mind blowing
Title: Very good   
Name: Deepak Jethani
Date: 7/10/2007 1:50:08 PM
Comment:
the article above is very good and information given might prove to be very useful for the programmers like me and many others. Thank u for the information.
Title: Fine   
Name: Fine
Date: 7/10/2007 1:48:16 PM
Comment:
Thank u for the Information given above. That is very useful for me
Title: Congrats.   
Name: Pranjit Kalita
Date: 4/13/2007 4:00:21 AM
Comment:
Well Done By You
Title: Very Interisting   
Name: Kulwinder Singh
Date: 4/7/2007 7:40:40 AM
Comment:
I want more basics of .Net from your site . Me available at meet.chd@gmail.com
Title: Very Informative.   
Name: Akshay
Date: 1/31/2007 8:11:50 AM
Comment:
This site really helped me, in my difficult times. Thanks!
Title: I think this will just about do...   
Name: Sunny
Date: 1/23/2007 4:52:16 AM
Comment:
How about having different levels of articles? Like: Beginner, Advanced, Expert...

In that perspective, I would say this is a nice article for a beginner level...

Keep it up.
Title: Good!!   
Name: Sujit
Date: 1/4/2007 5:13:11 AM
Comment:
Thanks for the article.
Title: good   
Name: chintan vasani
Date: 10/19/2006 6:53:16 AM
Comment:
nice and very simple way u have explained but pls try to show some way to dynamically connect
Title: To the Point   
Name: CoolFool
Date: 7/14/2006 3:44:27 AM
Comment:
Thanks for the nice and precise article.
Title: Building Site Map   
Name: Amarnath Banerjee
Date: 6/8/2006 2:24:02 AM
Comment:
Excellent ,Thanks a Lot Anand ,ur article has been really helpful.Could appreciate if you could some more articles on the Security front of .net pages using the login control
Title: Thanks   
Name: Kunal
Date: 3/9/2006 3:25:41 AM
Comment:
Thanks for the article. "G S" whats your problem dude, the article is good so what if its is copied. The main thing here, people are being benefited. Grow Up.
Title: Hmm..   
Name: Ho
Date: 10/4/2005 3:47:54 AM
Comment:
Too bad I never try VisualStudio 2005
Hixz...

Anyway, thxz 4 the article
Title: Very Cool!   
Name: sharpdev
Date: 8/11/2005 10:24:31 AM
Comment:
hope to find more useful articles, bit advanced for some excietments, really thanks!! nice work :)
Title: Cool!   
Name: Andreas Kraus
Date: 7/21/2005 3:52:02 PM
Comment:
Thanks for this useful introduction to Sitemaps!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 4/23/2014 4:02:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search