How to set up a breadcrumb in an ASP.net page

折月煮酒 提交于 2019-11-29 14:45:48

This currently works for me.. I have lots more code in my Page_Load but this is the important piece

in my current MasterPages Pre-Render Event I have a method called

 protected void Page_PreRender(object sender, EventArgs e)
 {
     SetNavigationLabel();
 }

Then I setup this inside of the Page_Load()

protected void Page_Load(object sender, EventArgs e)
{   
    var pageUrl = GetCurrentPageName();
}

private void SetNavigationLabel()
{
    RadMenu NavigationMenu = (RadMenu)this.FindControl("RadMenu1");
    foreach (RadMenuItem m in NavigationMenu.Items)
    {
        if (Request.Url.AbsoluteUri.ToLower() == Server.MapPath(Request.Url.AbsolutePath.ToLower()) || m.Selected)
        {
            string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sPagePath);
            string sPageName = "~/" + oFileInfo.Name;
            oFileInfo = null;
            var navName1 = NavigationMenu.FindItemByUrl(Request.RawUrl);
            var navName = navName1.Text;
            lblNavTitle.Text = navName;
            ((IDisposable)NavigationMenu).Dispose();
            break;
        }
    }
}

public string GetCurrentPageName()
{
     var sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
     FileInfo oInfo = new FileInfo(sPath);
     var sReturn = oInfo.Name;
     oInfo = null;
     return sReturn;
}
11nallan11

In reference to your question here: How to use Bootstrap style of BreadCrumb with my ASP.NET menu?

The SiteMapPath acts as the <ul/> tag in the HTML rendering. So to use the method there, your structure would likely be something like this:

<div class="bcHolder brClear"> <!-- BC MAIN -->
    <div class="innerBreadCrumb"> <!-- INNER BC -->
        <asp:SiteMapPath ID="SiteMap1" 
        runat="server"
        PathSeparator=" / " 
        ParentLevelsDisplayed="1" 
        PathDirection="RootToCurrent"
        ShowToolTips="true">
        <CurrentNodeStyle CssClass="current-node"></CurrentNodeStyle>
        <NodeTemplate>
            <li><a href="default.aspx" title="Home"><img src="theImages/homeIcon.gif" alt="Home" title="Home" class="home" /></a></li>
            <li id="bc_fp"><a href="find_provider.aspx" title="Find a Provider">Find a Provider</a></li>
            <!--<li>{ON THE CURRENT PAGE TEXT/URL</li>-->
            </NodeTemplate>
        </asp:SiteMapPath>
    </div> <!-- INNER BC -->
</div> <!-- BC MAIN -->

And then add the javascript you need to resolve the hyperlink of CurrentNode.

Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!