日期:2014-05-17  浏览次数:20496 次

“Repeater”控件显示的数据如何截取长度。
数据表“nrong”中有列“title”中储存文章的标题长度50个字符,现在在页面中用“Repeater”控件显示标题,希望只显示前40个字符,要怎样操作呀?

------解决方案--------------------
C# code

写个扩展方法吧
public static Left(this string source, int subLength)
{
    return source == null ? string.Empty : source.SubString(0, source.Length >=subLength ? subLength : source.Length);
}
<%# ((string)Eval("title")).Left(40) %>
或
<%# new string(((string)Eval("title") ?? string.Empty).Take(40).ToArray())%>

------解决方案--------------------
写个方法
 public string GetTitle(object title, int length)
{
string strTitle = title.ToString();
if (strTitle.Length > length)
{
strTitle = strTitle.Substring(0, length) + "...";
}
return strTitle;
}

数据绑定时:
<%#GetTitle(Eval("Title"), 12)%>