Tuesday, January 16, 2007

In most of the ASP.NET projects I have been working on, I have needed a way to easily navigate to any page in the solution. To accomplish this I usually created some kind of User Control with links to all pages. If the project was small I usually entered the links by hand. For bigger projects I have used a Repeater control and filled it with data from the file system.
The method used to get the data from the file system looks more or less like this:

public string[] GetAllPages()
{
    int applicationFolderNumberOfCharacters = HttpRuntime.AppDomainAppPath.Length;
    List<string> pages = new List<string>();
    string[] files = Directory.GetFiles(
      HttpRuntime.AppDomainAppPath, "*.aspx", SearchOption.AllDirectories); return Array.ConvertAll<string, string>(files, delegate(string file) { return "~/" +
         file.Substring(applicationFolderNumberOfCharacters).Replace(@"\", "/"); }); }

On the Page or User Control the Repeater I use looks as follows:

<asp:Repeater ID="Links" runat="server">
   
<ItemTemplate>
      
<a runat="server" href='<%# Container.DataItem %>'><%# Container.DataItem%></a><br />
   
</ItemTemplate>
</
asp:Repeater>

If you put it on the Master Page, you will get access to every page from every page, which is great during development. Just don't forget to remove it from the production code.

kick it on DotNetKicks.com