asp.net - 通过代码隐藏将属性添加到 html 标记会导致错误类型 (System.Web.UI.HtmlControls.HtmlElement) 不兼容

标签 asp.net vb.net webforms code-behind html

当我尝试将属性添加到我的母版页中的 html 标记时:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" id="primtag" runat="server">

就像后面的代码一样:

prmimaryhtml_tag.Attributes.Add("lang", "en")

我收到这个错误:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: The base class includes the field 'primtag', but its type (System.Web.UI.HtmlControls.HtmlElement) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).

Source Error: 


Line 4:  
Line 5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Line 6:  <html xmlns="http://www.w3.org/1999/xhtml" id="primtag" runat="server">
Line 7:  <head id="Head1" runat="server">
Line 8:      <title></title>

Source File: /main.master    Line: 6 

我还检查了 main.master.designer.vb,它包含这一行:

Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement

现在,最奇怪的是:这段代码在我的本地机器上工作正常,但在我的生产服务器上却不行。我也不知道是什么导致了这种不同的行为。

我也看了这个帖子:set pages main html tag in c#

更新 当我的情况如下:

main.master.designer.vb

 Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement

main.master.aspx

<html xmlns="http://www.w3.org/1999/xhtml" ID="primtag" runat="server">

main.master.aspx.vb

评论这一行:

'primtag.Attributes.Add

我得到:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: The base class includes the field 'primtag', but its type (System.Web.UI.HtmlControls.HtmlElement) is not compatible with the type of control (System.Web.UI.HtmlControls.HtmlGenericControl).

Source Error: 


Line 4:  
Line 5:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Line 6:  <html xmlns="http://www.w3.org/1999/xhtml" ID="primtag" runat="server">
Line 7:  <head id="Head1" runat="server">
Line 8:      <title></title>

之后,我尝试将 main.master.designer.vb 文件中的那一行从 Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement 更改为 Protected WithEvents primtag作为 Global.System.Web.UI.HtmlControls.HtmlGenericControl 在生产服务器上我得到:

对象引用未设置为对象的实例。

更新 2

可能和我的方案编译有关,我也查了这些帖子:

http://briancaos.wordpress.com/2013/08/07/system-web-ui-htmlcontrols-htmliframe-is-not-compatible-with-the-type-of-control-system-web-ui-htmlcontrols-htmlgenericcontrol/
https://connect.microsoft.com/VisualStudio/feedback/details/736011/iframe-parser-error
http://forums.asp.net/t/1884696.aspx?help+required+with+iframes+on+aspx
http://support.microsoft.com/kb/941824
http://forums.asp.net/t/1686949.aspx

然后我在本地机器上检查我的 IIS7.5 设置,那里的应用程序池设置为 ASP.NET4.0,与我的生产服务器上的相同。 我在 Visual Studio 中的目标框架编译设置为 .NET Framework 4.5。

希望这能提供更多见解,使其发挥作用。请帮助:)

最佳答案

每个动态添加的属性可能都应该以任何特殊(例如,“xml”)前缀开头。

检查 this线程,这可能会有所帮助。

关于asp.net - 通过代码隐藏将属性添加到 html 标记会导致错误类型 (System.Web.UI.HtmlControls.HtmlElement) 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060196/

相关文章:

c# - 如何在 C# 中的复选框列表中添加文本框?

c# - 如何在 ASP.NET 中动态创建的 TreeView 中添加 URL?

javascript - 在 ASP.NET 中单击提交按钮时使表单不刷新

vb.net - System.IO.Directory.Getfiles Silverlight 4 不工作

WPF ComboBox 绑定(bind)未按预期工作

c# - 是否有一种总是在网络表单中调用的方法?

asp.net-mvc - 具有相同 MachineKey 的 ASP.NET MVC 和 Webforms 应用程序之间的 OWIN 身份验证 cookie 共享

c# - 在 .Net 4.5 中混合使用 Forms 和 Windows 身份验证

c# - 在 ASP.NET 应用程序中创建具有复杂规则的向导式导航系统的有效且可维护的方法是什么?

.net - 扩展 ImageMagickNet