您的位置:首页 > 其它

DataList中如何格式化字符串(页面显示评论内容显示)

2009-03-04 18:25 260 查看
1.后台 早就为datalist绑定了数据源
2.然后在后台还提供我刚才的那个截取方法
//格式化评论内容(格式化就用这个)
protected string Famart(string str)
{
int numId = 0; //定义行数
int wordsInRow = 20; //定义一行显示多少个字符
String returnStr = ""; //格式化之后的字符
//循环控制显示的内容及格式
for(int i = 0; i < Math.Ceiling(str.Length/(float)wordsInRow); i ++)
{
//控制每行显示的内容
if (str.Length - numId * wordsInRow >= wordsInRow)
returnStr += "行:<font color=red>" + (numId+1)+"</font>:"+str.Substring(numId * wordsInRow,wordsInRow) + "<BR>";
else
returnStr += "行:<font color=red>" + (numId+1) + "</font>:" + str.Substring(numId * wordsInRow, str.Length - numId * wordsInRow) + "<BR>";
numId++;
}
return returnStr;
}

//格式化评论内容(截取就用这个)
protected string Famart(string str)
{
if (str.Length > 6)
{
return str.Substring(0, 5) + "...";
}
else
{
return str;
}
}
3.最后在前台就以“<%# Famart(Eval("CommentText").ToString())%> ” 去调用 刚才这个方法(传入参数)

<asp:DataList ID="DlCommentMess" runat="server" Width="128%" DataKeyField="CommentID" RepeatColumns="2">
<ItemTemplate>
<table align="center" width="75%">
<tr>
<td align="left">
<%--调用方法--%>
<%# Famart(Eval("CommentText").ToString())%>
</td>
</tr>
<tr>
<td align="left">
网名:<%#Eval("CommentFromName")%><br />
E-mail:<%#Eval("CommentFromWebURL")%><br />
评论时间:<%#Eval("CommentDateSubmitted")%>
<hr color="#CCCCCC" noshade="noshade" size="2">
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐