时虽然有点棘手,这是一个解决方案,实际上消除了跨度和呈现一个干净的列表。
首先,通过修改SiteMapPath
摆脱那些多余的跨度。我已经派生出一个类NakedSiteMapPath
这样做。如果需要,它仍允许在模板中使用显式跨度:
/// <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);
}
}
因此,跨度消失了。要有自定义链接,如li
元素,您需要使用模板,正如其他人已经提出的那样。下面是与NakedSiteMapPath
为例ASPX页面部分:
<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>
这是除了伟大的,一切都在节点显示出来:/ – SearchForKnowledge