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): 20753/ 10

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

thanks a lot buddy.
Title: thanks for better understanding   
Name: Parthiv dave
Date: 2008-06-17 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: 2008-05-30 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: 2008-05-23 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: 2008-04-07 6:00:04 AM
Comment:
Simple and clear - great place to start!
Title: FeedBack   
Name: UmangPAtel
Date: 2008-03-08 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: 2008-03-07 11:51:23 AM
Comment:
Ya It's Good For Us ...
Thank's FOr Giving Informataion
Title: WOW !!!!!!! !!!!!   
Name: dhaval
Date: 2008-02-26 9:30:51 AM
Comment:
it's nice get ...........
Title: Thank you very much   
Name: srikanth
Date: 2008-02-23 3:59:55 AM
Comment:
Awesome information.
Title: Thanks   
Name: Anitha
Date: 2008-02-04 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: 2008-01-31 6:29:59 AM
Comment:
Thanks & a lot
Title: sitemap   
Name: thirumurugan
Date: 2008-01-22 7:22:11 AM
Comment:
its factastic ,nice
Title: Building Sitemap   
Name: Sanjay David
Date: 2007-12-13 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: 2007-10-22 5:01:52 AM
Comment:
Its good but too basic.Expecting some advanced articles.
Title: easy   
Name: madhes
Date: 2007-10-13 10:22:40 AM
Comment:
easy to understand, thank u for given us,
Title: Easy to Understand   
Name: Pranav
Date: 2007-09-06 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: 2007-09-03 12:11:47 PM
Comment:
It's mind blowing, great
Title: Too Much   
Name: Murthy
Date: 2007-08-03 3:51:44 AM
Comment:
It's mind blowing
Title: Very good   
Name: Deepak Jethani
Date: 2007-07-10 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: 2007-07-10 1:48:16 PM
Comment:
Thank u for the Information given above. That is very useful for me
Title: Congrats.   
Name: Pranjit Kalita
Date: 2007-04-13 4:00:21 AM
Comment:
Well Done By You
Title: Very Interisting   
Name: Kulwinder Singh
Date: 2007-04-07 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: 2007-01-31 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: 2007-01-23 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: 2007-01-04 5:13:11 AM
Comment:
Thanks for the article.
Title: good   
Name: chintan vasani
Date: 2006-10-19 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: 2006-07-14 3:44:27 AM
Comment:
Thanks for the nice and precise article.
Title: Building Site Map   
Name: Amarnath Banerjee
Date: 2006-06-08 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: 2006-03-09 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: 2005-10-04 3:47:54 AM
Comment:
Too bad I never try VisualStudio 2005
Hixz...

Anyway, thxz 4 the article
Title: Very Cool!   
Name: sharpdev
Date: 2005-08-11 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: 2005-07-21 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-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 5:48:33 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search