How can I turn the output of asp:SiteMapPath into a list?

前端 未结 5 2446
借酒劲吻你
借酒劲吻你 2021-02-20 04:52

I\'m extremely unfamiliar with both .NET and VB.NET and can\'t quite figure out how to do this. Say I have code like this:

&
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 05:35

    While a little involved, this is a solution that actually removes the spans and renders a clean list.

    First, get rid of those excess spans by modifying the SiteMapPath. I have derived a class NakedSiteMapPath that does this. It still allows the usage of explicit spans in templates, if needed:

    /// 
    ///     A SiteMapPath, that does not render span elements.
    /// 
    /// 
    ///     To still allow explizit spans inside the node templates, immediately prefix the opening and closing span elements
    ///     with the literal
    ///     prefix "" (without the double quotes)
    ///     Example:
    ///     
    ///     
    ///         
    /// 
    ///     Those spans (opening and closing) will be kept, but the prefix removed in the rendered output.
    /// 
    /// 
    ///     The MSDN doc has a nice example about a customized breadcrumb with a dropdown menu here:
    ///     https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sitemappath%28v=vs.110%29.aspx
    /// 
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    [ToolboxData("<{0}:NakedSiteMapPath runat=server>")]
    public class NakedSiteMapPath : SiteMapPath {
        /// 
        ///     Outputs server control content to a provided  object and stores
        ///     tracing information about the control if tracing is enabled.
        /// 
        /// The  object that receives the control content.
        public override void RenderControl(HtmlTextWriter writer) {
            //Render to a local string, then remove all unnecessary spans
            StringBuilder myStringBuilder = new StringBuilder();
            TextWriter myTextWriter = new StringWriter(myStringBuilder);
            HtmlTextWriter myWriter = new HtmlTextWriter(myTextWriter);
            base.RenderControl(myWriter);
    
            string html = myStringBuilder.ToString();
    
            //Remove all spans, except those opening and closing spans wich have been marked with the literal comment ""
            const string matchOpenExceptSkipPrefix = @"(?)";
            const string matchCloseExceptSkipPrefix = @"(?)";
            html = Regex.Replace(html, matchOpenExceptSkipPrefix, String.Empty);
            html = Regex.Replace(html, matchCloseExceptSkipPrefix, String.Empty);
            html = html.Replace(@"", String.Empty);
    
            //finally, write the naked html out.
            writer.Write(html);
        }
    }
    

    With that, the spans are gone. To have custom links, like the li elements, you will need to use the templates, as others already have proposed. Here's an example ASPX page part with the NakedSiteMapPath:

    
    

提交回复
热议问题