动态生成Table内文字换行。

后台动态生成table,并把td内的文字进行换行。

前台:

<body style="100%;height:540px;margin-left:0px;margin-top:0px;overflow:scroll">
<form id="form1" style="font-size:10pt" runat="server">
<div >
    <table width="95%"  border="0"  align="center" cellpadding="3" style="table-layout:fixed;" cellspacing="1" bgcolor="#A2C1C8" id="tbContent" runat="server">
        <tr>
            <td colspan="3" style="background-color:#deebef" align="center" >流程开始</td>
        </tr>
    </table>
    
</div>
</form>
</body>

后台:

  protected void show()
    {
        try
        {
            string eventinfoid = Server.UrlDecode(Request.QueryString["eventinfoid"].ToString());
            DataTable dtFlowOperationState = new DataTable();
            dtFlowOperationState = flowBLL.GetList(" eventid='" + eventinfoid + "' order by newaddtime asc").Tables[0];
            int i = 1;
            foreach (DataRow dr in dtFlowOperationState.Rows)
            {
                HtmlTableRow row = new HtmlTableRow();
                HtmlTableCell cell = new HtmlTableCell();
                cell.Align = "center";
                cell.BgColor = "#f7f7f7";
                cell.InnerText = "" + i.ToString() + "";
               
                i++;
                row.Cells.Add(cell);

                cell = new HtmlTableCell();
                cell.Align = "left";
                cell.BgColor = "#f7f7f7";
               
                string strTemp = "";
                string UserName = getUserNameByID(dr["dealpeopleid"].ToString());
                string dealtypeO = dr["dealtype"].ToString();
                string delatime = dr["dealtime"].ToString();
                string newaddtime = dr["newaddtime"].ToString();
                if (!string.IsNullOrEmpty(delatime) && !string.IsNullOrEmpty(newaddtime))
                {
                    strTemp += UserName + "[<font color='#008200'>" + dealtypeO + "&nbsp;用时:" + getTimeCount(Convert.ToDateTime(delatime), Convert.ToDateTime(newaddtime)) + "</font>]<br />";
                    strTemp += "开始于:" + newaddtime + "<br />";
                    strTemp += "步骤结束于:" + delatime + "<br />";
                }
                else
                {
                    strTemp += UserName + "[<font color='#008200'>" + dealtypeO + "&nbsp;用时:0天0时0分0秒" +"</font>]<br />";
                    strTemp += "开始于:" + newaddtime + "<br />";
                    strTemp += "步骤结束于:正在处理中....<br />";
                }
                cell.InnerHtml = strTemp;
                row.Cells.Add(cell);

                cell = new HtmlTableCell();
                cell.Align = "left";
                cell.VAlign = "top";
                cell.BgColor = "#f7f7f7";
                cell.Style.Add("word-wrap","break-word");//控制table换行
                cell.Width = "400";
                cell.InnerText = "意见:" + dr["dealcontext"].ToString();
                row.Cells.Add(cell);
                this.tbContent.Rows.Add(row);
            }
        }
        catch (System.Exception ex)
        {
            //ProcessException("JHLOA_New", "WorkFlow_Flow_PreviewFlowView", "show", ex.Message);
            //Response.Redirect("../../error.aspx?id=3");
        }
    }
    #region 获取时间差
    public string getTimeCount(DateTime onTime, DateTime offTime)
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            TimeSpan ts = onTime - offTime;
            if (ts.Days > 0)
            {
                sb.Append(ts.Days.ToString());
                sb.Append("");
            }
            sb.Append(ts.Hours.ToString());
            sb.Append("小时");
            sb.Append(ts.Minutes.ToString());
            sb.Append("");
            sb.Append(ts.Seconds.ToString());
            sb.Append("");
        }
        catch (System.Exception ex)
        {

        }
        return sb.ToString();
    }
    #endregion

其中:

cell.Style.Add("word-wrap","break-word");//控制table换行
cell.Width = "400";

控制意见进行换行。

原文地址:https://www.cnblogs.com/smile-wei/p/3361533.html