【问题标题】:Table size with HTML table code in c# [closed]c#中带有HTML表格代码的表格大小[关闭]
【发布时间】:2012-07-12 04:11:28
【问题描述】:

这是我在 C# 代码中的 html 代码

tosend = "<p><tr><th>The directory searched:  " + path + " </th><th> between the dates " + fdate + " and  " + ldate + "</th></tr> <br> </p> <tr><th>File Name</th>  <th>File Date</th>  <th>File Hash</th>  <th>Beamer Date</th>  <th>Beamer Hash</th> <th>Status</th></tr>";
for (int i = 0; i < thexfiles.Length; i++)
{ 
    tosend = tosend + "<tr><td>" + thexfiles[i] + "</td><td>" + filedate[i] + "</td><td>" + hashList[i] + "</td><td>" + thexdates[i] + "</td><td>" + beamerHash[i] + "</td><td>" +status[i] + "</td></tr>"; 
}
tosend = "<html><table border="+1+">" + tosend + "</table></html>"; 

这是一个外观链接:Click Here

我想做的是,

  1. 我想扩展第一行,使其与其他行的大小相同,
  2. 您可以看到日期行和状态行的高度太大,我希望它们看起来像一条线,就像展开一样。

这就是我所需要的,到目前为止我已经尝试过正常的html宽度-高度等,我可能因为错误的符号而无法成功。

【问题讨论】:

  • 到目前为止你尝试过什么?已阅读 th 标签上的文档? w3schools.com/tags/tag_th.asp
  • 我尝试定义它们的宽度和高度,高度似乎适用于第一行,但宽度没有。我已经检查了该链接
  • 您的 HTML 无效。 &lt;tr&gt; 不能是 &lt;p&gt; 的子级。

标签: c# html html-table


【解决方案1】:

正如@David所说,HTML的构造是无效的。

  • 在“tr”中间有一个“p”和“br”标签
  • 标签也不见了。

尝试以这种方式构建表格。我把 HTML 代码单独放了

 <html>
<body>
<table border=1>
     <tr>
        <th colspan="3">The directory searched</th>
        <th colspan="3"> between the dates </th>
     </tr>
     <tr>
        <th>File Name</th>
        <th>File Date</th>
        <th>File Hash</th>
        <th>Beamer Date</th>
        <th>Beamer Hash</th>
        <th>Status</th>
     </tr>

     <tr>
        <td>thexfiles[i]</td>
        <td>filedate[i]</td>
        <td>hashList[i]</td>
        <td>thexdates[i]</td>
        <td>beamerHash[i]</td>
        <td>status[i]</td>
    </tr>
</table>
</body>
 </html>

【讨论】:

  • colspan 解决了我的第一行问题,但是日期列总是返回一个字符串,它们之间有一个空格,所以它传递到下一行,有没有办法解决这个问题?
  • 尝试为单元格添加 CSS 样式 white-space: nowrap
【解决方案2】:
  1. 对于第一行的单元格,使用

    如果您希望它以 5 列的形式出现。
  2. 用于您希望在 1 行中包含的单元格。

    【讨论】:

    【解决方案3】:

    您可以使用colspan="100%" 扩展第一行(或使用设定的列数)。除此之外,您只是想将太多数据挤入一个小区域。您必须缩短时间或删除一列才能使行高与您拥有的数据正常工作。您还可以强制较长的列为其他列提供更多空间,但也会导致它们换行。

    另请注意,行之间的&lt;br&gt;&lt;/&gt;p&gt; 标记不是有效的 HTML。

    tosend = "<tr><th>The directory searched:  " + path + " </th><th colspan='100%'> between the dates " + fdate + " and  " + ldate + "</th></tr><tr><th>File Name</th>  <th>File Date</th>  <th>File Hash</th>  <th>Beamer Date</th>  <th>Beamer Hash</th> <th>Status</th></tr>";
    
    for (int i = 0; i < thexfiles.Length; i++)
    {  
        tosend = tosend + "<tr><td>" + thexfiles[i] + "</td><td>" + filedate[i].ToShortDateString() + "</td><td>" + hashList[i] + "</td><td>" + thexdates[i].ToShortDateString() + "</td><td>" + beamerHash[i] + "</td><td>" +status[i] + "</td></tr>";  
    }
    tosend = "<html><table border="+1+">" + tosend + "</table></html>"; 
    

    【讨论】:

    • ToShortDateString() 不起作用
    • @tipi - "filedate[]" 是否持有 DateTime 对象?如果没有,您将不得不以其他方式截断时间。
    • 我在最初将日期添加到该字符串数组的地方使用它,它有效,非常感谢。现在它看起来更干净了。干杯。
    • 最后一个问题,当我尝试写一个在它们之间有空格的字符串时,它只是跳下一行,我怎样才能在一行中写而不让它跳过一行?
    • 如果您希望日期明确不中断(单行),那么您可以使用 CSS 属性:white-space: nowrap;
    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2011-11-05
    • 2010-12-19
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多