472,119 Members | 962 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

bread crumbs

hi
i am using asp.net with c#

i want to know how to creat bread crumbs for dynamically created pages

(i dont want to use site map because i think site map is only for static pages)

do help me in this regard

looking forward for a positive reply to the earliest :)
Jul 28 '06 #1
2 3747
sashi
1,754 Expert 1GB
Hi there,

i have attached some sample code segment below.. hope it helps you to get started.. take care..

the code..
Expand|Select|Wrap|Line Numbers
  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.  
  23. // Above are our variables that we can set:
  24. // Separator is the ‘>’ symbol 
  25. // RootName is the homepage anchor text 
  26. // directoryNameSpacer is the naming scheme for my directories, for example: search_engine_optimization or website_design (notice the URL in the address bar above) 
  27.  
  28.  
  29. public string PageTitle 
  30. {
  31.  get
  32.  {
  33.    return _PageTitle;
  34.  }
  35.  set
  36.  {
  37.   _PageTitle = value;
  38.  }
  39. }
  40. /// <summary> 
  41. /// Render this control to the output parameter specified.
  42. /// </summary>
  43. /// <param name="output"> The HTML writer to write out to </param>
  44. protected override void Render(HtmlTextWriter output)
  45. {
  46.   StringBuilder sbResult = new StringBuilder(); 
  47.   // sbResult StringBuilder will hold the breadcrumb navigation when done
  48.  
  49.   // get the url root, like www.domain.com
  50.   string strDomain = Page.Request.ServerVariables["HTTP_HOST"].ToString();
  51.   strDomain.Trim(); // Trim removes leading and trailing whitespace
  52.   sbResult.Append ( "<a href='http://" + strDomain + "'>" + RootName + "</a>" + Separator );
  53.  
  54.   // gets dir(s), like subdirectory/subsubdirectory/file.aspx
  55.   string scriptName = Page.Request.ServerVariables["SCRIPT_NAME"].ToString();
  56.   // find the last '/' and Remove the text after it as it's the file name
  57.   int lastSlash = scriptName.LastIndexOf('/'); // returns the # of chars. from right to /
  58.   string pathOnly = scriptName.Remove(lastSlash, (scriptName.Length - lastSlash));
  59.  
  60.   // create breadcrumb HTML for the directory name(s)
  61.   // We Remove the first "/" otherwise when you split the string the first item in array is empty
  62.   pathOnly = pathOnly.Substring(1); 
  63.   string[] strDirs = pathOnly.Split('/');
  64.   int nNumDirs = strDirs.Length;
  65.  
  66.   // URLs for breadcrumbs
  67.   string strURL = "";
  68.   for (int i=0; i<nNumDirs; i++)
  69.   {
  70.    strURL += "/"+strDirs[i];
  71.  
  72.    // convert underscores to spaces 
  73.    strDirs[i] = strDirs[i].Replace(directoryNameSpacer,' ');
  74.  
  75.    int counter = i+1;
  76.    if (counter != nNumDirs)
  77.    {
  78.     sbResult.Append ( "<a href='http://" + strDomain + strURL + "'>" + strDirs[i] + "</a>" + Separator );
  79.    }
  80.    else
  81.    {
  82.     // This is the last directory so don't tack on Separator 
  83.     sbResult.Append ( "<a href='http://" + strDomain + strURL + "'>" + strDirs[i] + "</a>" );    }
  84.   }
  85.   // write the PageTitle, pulled from the CodeBehind!
  86.   sbResult.Append ( " : " + this.PageTitle );
  87.  
  88.   output.Write ( sbResult.ToString() );
  89.  
  90.   }
  91.   }
  92. }
  93.  
The .ascx Page Code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Register TagPrefix="bc" Namespace="BreadCrumbs" Assembly="BreadCrumbs" %> 
  2.  
  3. <bc:ctrlBreadCrumbs id="bc1" runat="server" /> 
  4.  
The Code Behind For The .ascx Page:
Expand|Select|Wrap|Line Numbers
  1. protected BreadCrumbs.ctrlBreadCrumbs bc1;
  2.  
  3. private string _strHeaderText;
  4. public string HeaderText
  5. {
  6.   get
  7.   {
  8.    return _strHeaderText;
  9.   }
  10.   set
  11.   {
  12.    _strHeaderText = value;
  13.   }
  14. }
  15.  
  16. private void Page_Load(object sender, System.EventArgs e)
  17. {
  18.   // Put user code to initialize the page here
  19.   bc1.PageTitle = HeaderText;
  20. }
  21.  
Creating Our Web Pages (.aspx)
Expand|Select|Wrap|Line Numbers
  1. <title><asp:literal id="lblPageTitle" runat="server" /></title> 
  2.  
Your code behind would look like this:
Expand|Select|Wrap|Line Numbers
  1. public class CodeBehind_for_page: System.Web.UI.Page
  2. {
  3.   protected System.Web.UI.WebControls.Literal lblPageTitle;
  4.   protected NameSpace.headerBreadCrumb header;
  5.  
  6.   private void Page_Load(object sender, System.EventArgs e)
  7.   {
  8.    // Put user code to initialize the page here
  9.    string PageTitle = "ASP.NET Breadcrumbs with C#";
  10.    lblPageTitle.Text = PageTitle;
  11.    header.HeaderText = PageTitle;
  12.   }
  13.  
Jul 28 '06 #2
pac303
1
Hi Sashi

I've been trying to get the code you posted working, but can't in Web Matrix, Dreamweaver or Visual Studio. Could you send me the individual files which should be created from this code or post working examples including all the HTML in each page. pac303@yahoo.co.uk

Thanks in advance, Paul.
Aug 14 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Robert Guildner | last post: by
1 post views Thread by Irshad | last post: by
1 post views Thread by Boris Ammerlaan | last post: by
2 posts views Thread by -=Chris=- | last post: by
2 posts views Thread by Eric Lindsay | last post: by
1 post views Thread by Piotr Nowak | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.