473,881 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bread crumbs

1 New Member
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 3931
1,754 Recognized Expert Top Contributor
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;
  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) 
  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
  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 );
  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));
  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;
  66.   // URLs for breadcrumbs
  67.   string strURL = "";
  68.   for (int i=0; i<nNumDirs; i++)
  69.   {
  70.    strURL += "/"+strDirs[i];
  72.    // convert underscores to spaces 
  73.    strDirs[i] = strDirs[i].Replace(directoryNameSpacer,' ');
  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 );
  88.   output.Write ( sbResult.ToString() );
  90.   }
  91.   }
  92. }
The .ascx Page Code:
Expand|Select|Wrap|Line Numbers
  1. <%@ Register TagPrefix="bc" Namespace="BreadCrumbs" Assembly="BreadCrumbs" %> 
  3. <bc:ctrlBreadCrumbs id="bc1" runat="server" /> 
The Code Behind For The .ascx Page:
Expand|Select|Wrap|Line Numbers
  1. protected BreadCrumbs.ctrlBreadCrumbs bc1;
  3. private string _strHeaderText;
  4. public string HeaderText
  5. {
  6.   get
  7.   {
  8.    return _strHeaderText;
  9.   }
  10.   set
  11.   {
  12.    _strHeaderText = value;
  13.   }
  14. }
  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. }
Creating Our Web Pages (.aspx)
Expand|Select|Wrap|Line Numbers
  1. <title><asp:literal id="lblPageTitle" runat="server" /></title> 
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;
  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.   }
Jul 28 '06 #2
1 New Member
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

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

Similar topics

by: Robert Guildner | last post by:
I am new to JavaScript and I am building a Web page in which I want to use a navigational/informational feature that I have seen called bread crumbs. It looks like: home>>one level>>next level>the page you are on Often I see it with different colors for the history and the current page. I would appreciate any help. Thanks!
by: Irshad | last post by:
Hi Al If anyone has worked with Bread Crumb or have Knowledge on that.Please I Need some samples of creating Bread Crumb in .NET Please let me know ASAP Thank -Irshad
by: gb | last post by:
Within a form, the usual way of submitting it is to use e.g. <INPUT TYPE="submit" VALUE="Send "> This produces the standard button. Is there an easy way to portray this as though it were a simple hypertext link? Second question: Is there any way to customize a submit button other than the text that it displays? i.e. can I change the colour and/or size?
by: Boris Ammerlaan | last post by:
About once a week, I post an FAQ pointer in this newsgroup under the same heading as this post. That pointer has two purposes: 1. To keep regulars from having to post the standard answers to standard questions again and again; if you have a standard question, you might save yourself some time by first reading the available material for this group (i.e. FAQs, tutorials & references). 2. To let posters know what other newsgroups exist...
by: -=Chris=- | last post by:
Hello all, I've designed a custom bread crumb control for several of my asp.net projects. The default property of this control is a custom HyperLinkCollection I've created, which contains, you guessed it, HyperLink objects. All of it works great if I create/assign the links at runtime in code. I'd like to be able to assign links at design time, which is possible using the properties window, but currently, they do not carry over to the...
by: Eric Lindsay | last post by:
I have been trying to do a CSS liquid layout imitating a frame, using position: fixed for header, footer, and side navigation, and a fixed background image. Page is valid HTML 4.01 Strict, and is at http://www.ericlindsay.com/palmtop/palmnote.htm and the CSS is valid (with colour warnings but no errors) at http://www.ericlindsay.com/palmtop/palmtoptest.css All the other pages in that directory use my four year old stylesheet (so don't...
by: Piotr Nowak | last post by:
Hi, Is there any bread crumbs component like this http://geekswithblogs.net/azamsharp/archive/2006/08/16/88197.aspx for asp.net 1.1 ? I need to show my users place where they are at any moment of using the web application. bread crumbs is what i am looking for, any suggestions ? greets.
by: AAaron123 | last post by:
Is there a method of making a sitemapnode not visible and still have it's title appear in the bread crumbs? The reason for the invisible sitemapnode is so the page appears in the breadcrumbs (even though the page was accessed otherwise than with a menuitem) I can do it by creating a sitemapnode without a url and placing the sitemapnode that I want invisible under it. That way it appears in the bread crumbs but looks it like a top...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.