在c#里只要在字符串前加上@符号,变可以让字符串随意换行,如下代码所示:
Code
string stroutput=string.Format(@"<table width='100%' border='0' align='center' cellpadding='3' cellspacing='0' style='margin-top:10px;'>
<tr>
<td> 你好!<b>{0}</b> [ <a href='{5}' >个人信息</a> , <a href='http://forum.mkd.com/logout.aspx?userkey={1}'>退出</a> ]</td>
</tr>
<tr>
<td> 积分:<b>{2}</b> </td>
</tr>
<tr>
<td> 等级:<b>{3}</b></td>
</tr>
<tr>
<td><hr style='height:1px;border-top:0px; border-style:dashed; border-left:0px; border-right:0px; border-color:black;' /></td>
</tr>
<tr>
<td> 新短消息 <a href='http://forum.mkd.com/usercpinbox.aspx' target='_blank'><font style='font-weight:bold; color:#B90000;'>{4}</font></a> , <a href='http://forum.mkd.com/usercpsubscribe.aspx' target='_blank'>收藏夹</a> , <a href='http://forum.mkd.com/usercpprofile.aspx' target='_blank'>管理</a></td>
</tr>
</table>", string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(4, 8).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));
string stroutput=string.Format(@"<table width='100%' border='0' align='center' cellpadding='3' cellspacing='0' style='margin-top:10px;'>
<tr>
<td> 你好!<b>{0}</b> [ <a href='{5}' >个人信息</a> , <a href='http://forum.mkd.com/logout.aspx?userkey={1}'>退出</a> ]</td>
</tr>
<tr>
<td> 积分:<b>{2}</b> </td>
</tr>
<tr>
<td> 等级:<b>{3}</b></td>
</tr>
<tr>
<td><hr style='height:1px;border-top:0px; border-style:dashed; border-left:0px; border-right:0px; border-color:black;' /></td>
</tr>
<tr>
<td> 新短消息 <a href='http://forum.mkd.com/usercpinbox.aspx' target='_blank'><font style='font-weight:bold; color:#B90000;'>{4}</font></a> , <a href='http://forum.mkd.com/usercpsubscribe.aspx' target='_blank'>收藏夹</a> , <a href='http://forum.mkd.com/usercpprofile.aspx' target='_blank'>管理</a></td>
</tr>
</table>", string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(4, 8).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));
而javascript里是不支持字符串的换行的,所以要将stroutput这个字符串输出通常需要一行一行来,在这我用了string.Split的方法来实现避免一行一行的手工输出:
输出方法
private void write(HttpContext context, string line)
{
line = line.Remove(line.Length - 1, 1);
context.Response.Write(string.Format("document.write(\"{0}\");\r\n",line));
}
private void write(HttpContext context, string line)
{
line = line.Remove(line.Length - 1, 1);
context.Response.Write(string.Format("document.write(\"{0}\");\r\n",line));
}
处理
string stroutput=string.Format(@"<table width='100%' border='0' align='center' cellpadding='3' cellspacing='0' style='margin-top:10px;'>
<tr>
<td> 你好!<b>{0}</b> [ <a href='{5}' >个人信息</a> , <a href='http://forum.mkd.com/logout.aspx?userkey={1}'>退出</a> ]</td>
</tr>
<tr>
<td> 积分:<b>{2}</b> </td>
</tr>
<tr>
<td> 等级:<b>{3}</b></td>
</tr>
<tr>
<td><hr style='height:1px;border-top:0px; border-style:dashed; border-left:0px; border-right:0px; border-color:black;' /></td>
</tr>
<tr>
<td> 新短消息 <a href='http://forum.mkd.com/usercpinbox.aspx' target='_blank'><font style='font-weight:bold; color:#B90000;'>{4}</font></a> , <a href='http://forum.mkd.com/usercpsubscribe.aspx' target='_blank'>收藏夹</a> , <a href='http://forum.mkd.com/usercpprofile.aspx' target='_blank'>管理</a></td>
</tr>
</table>", string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(4, 8).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));
string[] aroutput =stroutput.Split('\n');
foreach (string temp in aroutput)
{
write(context, temp);
}
string stroutput=string.Format(@"<table width='100%' border='0' align='center' cellpadding='3' cellspacing='0' style='margin-top:10px;'>
<tr>
<td> 你好!<b>{0}</b> [ <a href='{5}' >个人信息</a> , <a href='http://forum.mkd.com/logout.aspx?userkey={1}'>退出</a> ]</td>
</tr>
<tr>
<td> 积分:<b>{2}</b> </td>
</tr>
<tr>
<td> 等级:<b>{3}</b></td>
</tr>
<tr>
<td><hr style='height:1px;border-top:0px; border-style:dashed; border-left:0px; border-right:0px; border-color:black;' /></td>
</tr>
<tr>
<td> 新短消息 <a href='http://forum.mkd.com/usercpinbox.aspx' target='_blank'><font style='font-weight:bold; color:#B90000;'>{4}</font></a> , <a href='http://forum.mkd.com/usercpsubscribe.aspx' target='_blank'>收藏夹</a> , <a href='http://forum.mkd.com/usercpprofile.aspx' target='_blank'>管理</a></td>
</tr>
</table>", string.IsNullOrEmpty(userInfo.Nickname) ? userInfo.Username : userInfo.Nickname, userInfo.Password.Substring(4, 8).Trim(), userInfo.Credits, UserGroups.GetUserGroupInfo(userInfo.Groupid).Grouptitle, userInfo.Newpmcount, Urls.UserInfoAspxRewrite(userInfo.Uid));
string[] aroutput =stroutput.Split('\n');
foreach (string temp in aroutput)
{
write(context, temp);
}
原理就是利用回车换行符来分割字符串,达到分行输出的目的。