Mentre un po 'coinvolto, questa è una soluzione che elimina in realtà le campate e rende una lista pulita.
Innanzitutto, eliminare gli span in eccesso modificando SiteMapPath
. Ho derivato una classe NakedSiteMapPath
che esegue questa operazione. Permette comunque l'utilizzo di spans espliciti nei template, se necessario:
/// <summary>
/// A SiteMapPath, that does not render span elements.
/// </summary>
/// <remarks>
/// To still allow explizit spans inside the node templates, immediately prefix the opening and closing span elements
/// with the literal
/// prefix "<!--KEEP NEXT SPAN-->" (without the double quotes)
/// Example:
/// <code>
/// <PathSeparatorTemplate><!--KEEP NEXT SPAN--><span class="icon icon--greater"><!--KEEP NEXT SPAN--></span>
/// </PathSeparatorTemplate>
/// </code>
/// Those spans (opening and closing) will be kept, but the prefix removed in the rendered output.
/// </remarks>
/// <devdoc>
/// 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
/// </devdoc>
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
[ToolboxData("<{0}:NakedSiteMapPath runat=server></{0}:NakedSiteMapPath>")]
public class NakedSiteMapPath : SiteMapPath {
/// <summary>
/// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter" /> object and stores
/// tracing information about the control if tracing is enabled.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter" /> object that receives the control content.</param>
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 "<!--KEEP NEXT SPAN-->"
const string matchOpenExceptSkipPrefix = @"(?<!\<\!--KEEP NEXT SPAN--\>)<span>";
const string matchCloseExceptSkipPrefix = @"(?<!\<\!--KEEP NEXT SPAN--\>)</span>";
html = Regex.Replace(html, matchOpenExceptSkipPrefix, String.Empty);
html = Regex.Replace(html, matchCloseExceptSkipPrefix, String.Empty);
html = html.Replace(@"<!--KEEP NEXT SPAN-->", String.Empty);
//finally, write the naked html out.
writer.Write(html);
}
}
Con ciò, gli span sono spariti. Per avere collegamenti personalizzati, come gli elementi li
, è necessario utilizzare i modelli, come altri già hanno proposto. Ecco un esempio parte pagina ASPX con il NakedSiteMapPath
:
<ol class="breadcrumb" role="navigation" aria-labelledby="pagebreadcrumbs">
<my:NakedSiteMapPath runat="server"
PathDirection="RootToCurrent"
RenderCurrentNodeAsLink="False">
<PathSeparatorTemplate><!--KEEP NEXT SPAN--><span class="icon icon--greater"><!--KEEP NEXT SPAN--></span></PathSeparatorTemplate>
<CurrentNodeTemplate>
<li class="active" aria-selected="true">
<asp:Literal
Text='<%# Eval("Title") %>'
runat="server" />
</li>
</CurrentNodeTemplate>
<NodeTemplate>
<li>
<asp:HyperLink
ID="lnkPage"
Text='<%# Eval("Title") %>'
NavigateUrl='<%# Eval("Url") %>'
ToolTip='<%# Eval("Description") %>'
runat="server" />
</li>
</NodeTemplate>
</my:NakedSiteMapPath>
</ol>
Questo è grande, tranne, tutto nel nodo si presenta:/ – SearchForKnowledge