【问题标题】:Why textBody.Append is only appending the table header to the first record not others?为什么 textBody.Append 只是将表头附加到第一条记录而不是其他记录?
【发布时间】:2020-04-11 17:46:04
【问题描述】:

我正在遍历数据集和 pikcing 字段并将其放入我使用电子邮件发送的 html 表中。它有效,但只有第一行是正确的,但不是下一行。为什么?

Dim textBody As New StringBuilder
textBody.Append("<p> <b> Dear " & dsEmails.Tables(0).Rows(0).Item("FullName") & ",</b> <br /> <br /> Kindly review the timesheets of employees who have partially filled the records for week" + PreviousWeek.ToString() + "</b> <br /><br /> <br /></p>").AppendLine()
textBody.Append("<br><table rules='all' bordercolor='#4d4c4d' border='1' bgcolor='#FFFFFF' cellpadding='8'  align='center' width='500'>
    <tr>
        <th>Employee</th>
        <th>TotalHours</th>
        <th>Week</th>
        <th>Year</th>
    </tr>
    <tr>").AppendLine()

For Each row As DataRow In CTRData.Tables(0).Rows

    textBody.Append("<td bgcolor='#da5903' style='height:  5px;' > " + row("Employee_Name").ToString() + "</td>").AppendLine()
    textBody.Append("<td bgcolor='#da5903' style='height:  5px;'>" + row("OverallHrs").ToString() + " </td>").AppendLine()
    textBody.Append("<td bgcolor='#da5903' style='height:  5px;' > " + row("Week").ToString() + " </td>").AppendLine()
    textBody.Append("<td bgcolor='#da5903' style='height:  5px;'>" + row("Year").ToString() + " </td>").AppendLine()
    textBody.Append("</tr></table>").AppendLine()

    Next

【问题讨论】:

  • 首先将第二个&lt;tr&gt; 放入 ForEach 循环中...现在它会生成无效的 html。

标签: html asp.net .net vb.net dataset


【解决方案1】:

要正确生成您的 html 标记,您的代码需要变成:

    Dim textBody As StringBuilder = New StringBuilder()

    textBody.AppendLine("<p>")
    textBody.AppendLine("    <b> Dear " & CStr(dsEmails.Tables(0).Rows(0).Item("FullName")) & ",</b> ")
    textBody.AppendLine("    <br /> <br /> Kindly review the timesheets of employees who have partially filled the records for week" & PreviousWeek.ToString() & " </b>")
    textBody.AppendLine("    <br /> <br /> <br /> <br />")
    textBody.AppendLine("</p>")


    ' Table init
    textBody.AppendLine("<table rules='all' bordercolor='#4d4c4d' border='1' bgcolor='#FFFFFF' cellpadding='8'  align='center' width='500'> ")

    textBody.AppendLine("<tr>")
    textBody.AppendLine("   <th>Employee</th>")
    textBody.AppendLine("   <th>TotalHours</th>")
    textBody.AppendLine("   <th>Week</th>")
    textBody.AppendLine("   <th>Year</th>")
    textBody.AppendLine("</tr>")


    For Each row As DataRow In CTRData.Tables(0).Rows
        textBody.AppendLine("<tr>")
        textBody.AppendLine("    <td bgcolor='#da5903' style='height:  5px;'>" & row("Employee_Name").ToString() & "</td>")
        textBody.AppendLine("    <td bgcolor='#da5903' style='height:  5px;'>" & row("OverallHrs").ToString() & " </td>")
        textBody.AppendLine("    <td bgcolor='#da5903' style='height:  5px;'>" & row("Week").ToString() & " </td>")
        textBody.AppendLine("    <td bgcolor='#da5903' style='height:  5px;'>" & row("Year").ToString() & " </td>")
        textBody.AppendLine("</tr>")
    Next


    textBody.Append("</table>").AppendLine()
    'Table closure

同时更改 table 和 td 标签中的所有属性,改用有效的 html5 属性,例如 style='border:solid 1px #333' 等......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多