格式DataList绑定内容?
我在做新闻系统时,用DataList: <%#DataBinder.Eval(Container.DataItem, "Title ")%> 绑定标题,但有时标题太长,所以只想显示前10个字.该如何操作?
------解决方案--------------------public string newContent(string Content)
{
if (Content.Length > 15 )
{
Content = Content.Substring(0,15) + "... ";
}
return Content;
}
------解决方案-------------------- <%# GetShortString(DataBinder.Eval(Container.DataItem, "Title ").ToString())%>
后台
public string GetShortString(string str)
{
if(str.length > 10)
return str.subString(0,7) + "... ";
else
return str;
}
------解决方案--------------------public int GetLength(string strString)
{
int m = 0;
if (strString.Length == 0)
return m;
for (int i = 0; i <= strString.Length-1; i++)
{
int intTemp = (short)strString.Substring(i, 1).ToCharArray(0, 1)[0];
if (intTemp > 127 || intTemp < 0)
m += 2;
else
m += 1;
}
return m;
}
StringOp sop = new StringOp();
if (sop.GetLength(str) > 82)
{
str = sop.StringOutOfRange(str, 72);
}
------解决方案-------------------- <%# DataBinder.Eval(Container.DataItem, "Title ").ToString().Length> 15?(DataBinder.Eval(Container.DataItem, "Title ").ToString().Substring(0,15)+ ".. "):DataBinder.Eval(Container.DataItem, "Title ").ToString() %>