By:Mark Angeletti, Published:2005-1-15

We create a C# class within its own namespace so that we may use the breadcrumb navigation on all pages of our website as well as other websites we create. Later we will create a Web User Control (.ascx) that can be easily dropped into the header of our pages.

C#:
  1. using System;
  2. using System.Web.UI;
  3. using System.Web.UI.WebControls;
  4. using System.ComponentModel;
  5. using System.Text;
  6. // We include the System.Text namespace so we can use the StringBuilder object later.
  7. namespace BreadCrumbs
  8. {
  9. /// <summary>
  10. /// Summary description for ctrlBreadCrumbs.
  11. /// </summary>
  12. public class ctrlBreadCrumbs : System.Web.UI.WebControls.WebControl
  13. {
  14. /// <summary>
  15. /// The 3 variables below, Separator, RootName, directoryNameSpacer can be changed to meet your needs
  16. /// PageTitle is pulled from the CodeBehind of the page
  17. /// </summary>
  18. public string Separator = "> ";
  19. public string RootName = "Home Page";
  20. public char directoryNameSpacer = '_';
  21. private string _PageTitle;
  22. // Above are our variables that we can set:
  23. // Separator is the ‘>’ symbol
  24. // RootName is the homepage anchor text
  25. // directoryNameSpacer is the naming scheme for my directories, for example: search_engine_optimization or website_design (notice the URL in the address bar above)
  26.  
  27. public string PageTitle
  28. {
  29.  get
  30.  {
  31.    return _PageTitle;
  32.  }
  33.  set
  34.  {
  35.   _PageTitle = value;
  36.  }
  37. }
  38. /// <summary>
  39. /// Render this control to the output parameter specified.
  40. /// </summary>
  41. /// <param name="output"> The HTML writer to write out to </param>
  42. protected override void Render(HtmlTextWriter output)
  43. {
  44.   StringBuilder sbResult = new StringBuilder();
  45.   // sbResult StringBuilder will hold the breadcrumb navigation when done
  46.  
  47.   // get the url root, like www.domain.com
  48.   string strDomain = Page.Request.ServerVariables["HTTP_HOST"].ToString();
  49.   strDomain.Trim(); // Trim removes leading and trailing whitespace
  50.   sbResult.Append ( "<a href='http://" + strDomain + "'>" + RootName + "</a>" + Separator );
  51.  
  52.   // gets dir(s), like subdirectory/subsubdirectory/file.aspx
  53.   string scriptName = Page.Request.ServerVariables["SCRIPT_NAME"].ToString();
  54.   // find the last '/' and Remove the text after it as it's the file name
  55.   int lastSlash = scriptName.LastIndexOf('/'); // returns the # of chars. from right to /
  56.   string pathOnly = scriptName.Remove(lastSlash, (scriptName.Length - lastSlash));
  57.  
  58.   // create breadcrumb HTML for the directory name(s)
  59.   // We Remove the first "/" otherwise when you split the string the first item in array is empty
  60.   pathOnly = pathOnly.Substring(1);
  61.   string[] strDirs = pathOnly.Split('/');
  62.   int nNumDirs = strDirs.Length;
  63.  
  64.   // URLs for breadcrumbs
  65.   string strURL = "";
  66.   for (int i=0; i<nNumDirs; i++)
  67.   {
  68.    strURL += "/"+strDirs[i];
  69.  
  70.    // convert underscores to spaces
  71.    strDirs[i] = strDirs[i].Replace(directoryNameSpacer,' ');
  72.  
  73.    int counter = i+1;
  74.    if (counter != nNumDirs)
  75.    {
  76.     sbResult.Append ( "<a href='http://" + strDomain + strURL + "'>" + strDirs[i] + "</a>" + Separator );
  77.    }
  78.    else
  79.    {
  80.     // This is the last directory so don't tack on Separator
  81.     sbResult.Append ( "<a href='http://" + strDomain + strURL + "'>" + strDirs[i] + "</a>" );    }
  82.   }
  83.   // write the PageTitle, pulled from the CodeBehind!
  84.   sbResult.Append ( " : " + this.PageTitle );
  85.  
  86.   output.Write ( sbResult.ToString() );
  87.  
  88.   }
  89.   }
  90. }

Book it:
  • del.icio.us
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon

Popular Articles

Top 10 Commentators


Subscribe to this feed! Subscribe by Email!

Random Bits Podcast

You need to download the Flash player from Adobe

Blogs Worth Reading