带有 col、colgroup、tbody 和 thead 的 HTML 表标记在 Visual Studio 2010 和 Visual Studio 2012 中引发编译错误 [英] HTML table tag with col, colgroup, tbody, and thead throws compile error in Visual Studio 2010 and Visual Studio 2012

查看:20
本文介绍了带有 col、colgroup、tbody 和 thead 的 HTML 表标记在 Visual Studio 2010 和 Visual Studio 2012 中引发编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 .NET 3.5 网站,带有包含 col、colgroup、tbody 和 thead 标记的 table 标记.这是一个带有 runat="server" 属性的服务器端标记.此表在 Visual Studio 2010 中运行良好,但在我安装 Visual Studio 2012 和 .NET 4.5 后,此标记现在无法在 Visual Studio 2010 和 Visual Studio 2012 中编译.(我都试过.)以下是编译器错误正在被抛出:

I have a .NET 3.5 web site with a table tag that contains col, colgroup, tbody and thead tags. This is a server side tag with the runat="server" attribute. This table was working fine in Visual Studio 2010, but after I installed Visual Studio 2012 and .NET 4.5, this tag now fails to compile in Visual Studio 2010 and in Visual Studio 2012. (I tried both.) Here are the compiler errors that are being thrown:

  • System.Web.UI.HtmlControls.HtmlTableRowCollection.Add(System.Web.UI.HtmlControls.HtmlTableRow)"的最佳重载方法匹配有一些无效的参数
  • 参数1":无法从System.Web.UI.HtmlControls.HtmlGenericControl"转换为'System.Web.UI.HtmlControls.HtmlTableRow'

以下是我正在使用的示例:

Here's an example of what I'm working with:

<table id="TestTable" runat="server">
    <colgroup>
        <col width="30%" />
        <col width="70%" />
    </colgroup>
    <thead>
        <tr>
            <td>Sample header 1</td>
            <td>Sample header 2</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Sample cell 1</td>
            <td>Sample cell 2</td>
        </tr>
        <tr>
            <td>Sample cell 3</td>
            <td>Sample cell 4</td>
        </tr>
    </tbody>
</table>

有谁知道如何解决这个问题,以便我们可以编译网站并继续工作?

Does anyone know how to fix this problem, so that we can get the site to compile and continue working?

推荐答案

在安装 Visual Studio 2012 和 .NET 4.5 之后,这似乎是网站的一个未记录的重大更改.我无法在 Microsoft 记录的 .NET 4.5 更改中找到对此的引用:http://msdn.microsoft.com/en-us/library/hh367887.aspx

This appears to be an undocumented breaking change for web sites after the install of Visual Studio 2012 and .NET 4.5. I am not able to find references to this in the .NET 4.5 changes documented by Microsoft: http://msdn.microsoft.com/en-us/library/hh367887.aspx

在研究问题后,以下似乎是损坏的 table 标记的可能解决方案.

After researching the problem, the following appear to be possible solutions to the broken table tag.

  1. 卸载 Visual Studio 2012 和 .NET 4.5.参考:服务器端 HTML 表tbody 未在 ASP.NET 4.5 中编译

我意识到这不一定是理想的解决方案,但如果下面的其他解决方案都无法轻松实施,您最终可能别无选择.此外,仅仅因为这是第一个条目,它不是我推荐的主要解决方案.这只是一种选择.

I realize that this is not necessarily an ideal solution, but if none of the other solutions below can be implemented easily, you may end up with no other choice. Also, just because this is the first entry, it is not what I am recommending as the primary solution. It's just an option.

将您的网站转换为 Web 应用程序.使用 web 应用程序时,带有 runat="server" 的表似乎可以编译文件.

Convert your web site into a web application. The table with runat="server" appears to compile file when using a web application.

进行这种转换还有其他好处,例如可以更轻松地针对 Web 应用程序中的代码编写单元测试.但是,您需要评估从网站转换为 Web 应用程序所涉及的工作,并且需要说服您的老板和同事您需要进行此更改.

There are additional benefits to doing this conversion, such as making it easier to write unit tests against the code inside your web application. However, you will need to evaluate the work involved with converting from a web site to a web application, and you will need to convince your bosses and fellow coworkers that you need to make this change.

检查表的服务器端代码(页面/控件背后的代码).您是否在服务器端代码中使用控件?如果没有,请删除 runat="server".然后页面编译得很好.

Check the server side code (code behind page/control) for the table. Are you using the control in the server side code? If not, remove runat="server". The page then compiles just fine.

<table id="TestTable">
    <colgroup>
        <col width="30%" />
        <col width="70%" />
    </colgroup>
    <thead>
        <tr>
            <td>Sample header 1</td>
            <td>Sample header 2</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Sample cell 1</td>
            <td>Sample cell 2</td>
        </tr>
        <tr>
            <td>Sample cell 3</td>
            <td>Sample cell 4</td>
        </tr>
    </tbody>
</table>

  • 您正在使用服务器端代码中的控件.移除 col 和 colgroup 标签,并将列样式移动到表格第一行的 td 或 th 单元格中.(列宽和样式从表格的第一行继承,例如,在第一个单元格上设置 width="40%" 会使该列中的所有单元格都具有 width="40%".)删除 thead 标记并将表中的所有 td 单元格更改为 th(表头)单元格.删除 tbody 标签.

  • You are using the control in the server side code. Remove the col and colgroup tags and move the column stylings into the td or th cells of the first row of the table. (Column width and stylings inherit from the first row down the table, so setting width="40%" on the first cell, for example, makes all cells in that column have width="40%".) Remove the thead tag and change all td cells in the table to th (table header) cells. Remove the tbody tag.

    <table id="TestTable" runat="server">
        <tr>
            <th width="30%">Sample header 1</td>
            <th width="70%">Sample header 2</td>
        </tr>
        <tr>
            <td>Sample cell 1</td>
            <td>Sample cell 2</td>
        </tr>
        <tr>
            <td>Sample cell 3</td>
            <td>Sample cell 4</td>
        </tr>
    </table>
    

  • 转换为使用带有 标记代码>控件.参考:如何在 ASP.NET 中创建 thead 和 tbody表?

  • Convert to using the <asp:Table> tag with <asp:TableHeaderRow> and <asp:TableRow> controls. Reference: How to create thead and tbody in ASP.NET Table?

    这篇关于带有 col、colgroup、tbody 和 thead 的 HTML 表标记在 Visual Studio 2010 和 Visual Studio 2012 中引发编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆