如何在字符串生成器内对锚链接标题进行排序

时间:2016-02-19 16:51:17

标签: c# html ektron

我有以下内容:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < contentList.Count;i++)
{
    sb.Append("<div class=\"justPad\">");
    sb.Append("<a class=\"defaultLinks\" ");
    sb.Append("href=" + contentList[i].Quicklink + " ");
    sb.Append("title=" + contentList[i].Title + ">" + contentList[i].Title + "</a>");
    sb.Append("</div>");
}
Label2.Text = sb.ToString();

结果是:

<span id="ctl00_BodyPlaceHolder_Label2">
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3198" title="Hammond, John">Hammond, John</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3205" title="Beezer, Michelle">Beezer, Michelle</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3208" title="Tops, John">Tops, John</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3209" title="Kew, Lauren">Kew, Lauren</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=352" title="Perez, Lance">Perez, Lance</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=353" title="Powell, French">Powell, French</a></div>
</span>

如您所见,显示顺序未排序。

如何根据contentList[i].Title对StringBuilder进行排序,最终结果为:

<span id="ctl00_BodyPlaceHolder_Label2">
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3205" title="Beezer, Michelle">Beezer, Michelle</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3198" title="Hammond, John">Hammond, John</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3209" title="Kew, Lauren">Kew, Lauren</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=352" title="Perez, Lance">Perez, Lance</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=353" title="Powell, French">Powell, French</a></div>
    <div class="justPad"><a class="defaultLinks" href="/pro.aspx?id=3208" title="Tops, John">Tops, John</a></div>
</span>

1 个答案:

答案 0 :(得分:1)

很简单 - 在构建链接列表之前对内容列表进行排序:

foreach (var content in contentList.OrderBy(c => c.Title))
{
    sb.Append("<div class=\"justPad\">");
    sb.Append("<a class=\"defaultLinks\" ");
    sb.Append("href=\"" + content.Quicklink + "\" ");
    sb.Append("title=\"" + content.Title + "\">" + content.Title + "</a>");
    sb.Append("</div>");
}

还要考虑使用interpolated strings来提高代码的可读性:

sb.Append($"<a class='defaultLinks' href='{content.Quicklink}' title='{content.Title}'>");

注意:如果要创建一些html,请考虑使用HtmlTextWriter。您可以写入字符串(与StringBuilder一样)或直接写入任何其他TextWriter以避免创建大内存字符串:

// using System.Web.UI

StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{
    writer.AddAttribute(HtmlTextWriterAttribute.Id, "ctl00_BodyPlaceHolder_Label2");
    writer.RenderBeginTag(HtmlTextWriterTag.Span);

    foreach (var content in contentList.OrderBy(c => c.Title))
    {
        writer.AddAttribute(HtmlTextWriterAttribute.Class, "justPad");
        writer.RenderBeginTag(HtmlTextWriterTag.Div);

        writer.AddAttribute(HtmlTextWriterAttribute.Class, "defaultLinks");
        writer.AddAttribute(HtmlTextWriterAttribute.Href, content.Quicklink);
        writer.AddAttribute(HtmlTextWriterAttribute.Title, content.Title);
        writer.RenderBeginTag(HtmlTextWriterTag.A);

        writer.WriteEncodedText(content.Title);
        writer.RenderEndTag();
        writer.RenderEndTag();
    }

    writer.RenderEndTag();
}