剛剛入門學習asp.net的朋友,都會碰到把大量帶有換行文本的內容顯示時,不會自動換行的問題。本人現在把解決這一問題真正有效的辦法告訴大家,共同學習:
在vb.net中:
1 function htmlcode()function htmlcode(byval fstring)
2 if fstring <> "" then
3 fstring = replace(fstring, chr(13), "")
4 fstring = replace(fstring, chr(10) & chr(10), "</p><p>")
5 fstring = replace(fstring, chr(10), "<br>")
6 htmlcode = fstring
7 end if
8 end function
9
使用范例:
contenttxt.text = htmlcode(rs.item("newscontent"))
注:.contenttxt為label標簽控件;rs.item("newscontent")為讀取數據庫表中的記錄集。
以上代碼可在我的.net博客系統中找到詳細代碼。
在c#中:
private string htmlcode(string tstring)
{
if (tstring != null)
{
tstring = tstring.replace("/r", "<br>");
tstring = tstring.replace(" ", " ");
return tstring;
}
else
{
return tstring="無內容";
}
}
使用范例:
this.contenttxt.text = htmlcode(newstab.rows[0]["contenttxt"].tostring());
注:.contenttxt為label標簽控件;newstab.rows[0]["contenttxt"].tostring()為讀取數據庫表中的記錄集。
以上代碼可在我的.net新聞系統中找到詳細代碼。
出處:李錫遠 blog
新聞熱點
疑難解答
圖片精選