通过代码隐藏结果​​向 html 标记添加属性会导致错误类型 (System.Web.UI.HtmlControls.HtmlElement) 不兼容

Flo*_*Flo 5 html vb.net asp.net webforms code-behind

当我尝试向母版页中的 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">
Run Code Online (Sandbox Code Playgroud)

就像后面的代码一样:

prmimaryhtml_tag.Attributes.Add("lang", "en")
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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 
Run Code Online (Sandbox Code Playgroud)

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

Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement
Run Code Online (Sandbox Code Playgroud)

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

我还看了这篇文章:set Pages main html tag in c#

更新 当我的情况如下时:

main.master.designer.vb

 Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement
Run Code Online (Sandbox Code Playgroud)

main.master.aspx

<html xmlns="http://www.w3.org/1999/xhtml" ID="primtag" runat="server">
Run Code Online (Sandbox Code Playgroud)

main.master.aspx.vb

评论了这一行:

'primtag.Attributes.Add
Run Code Online (Sandbox Code Playgroud)

我得到:

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>
Run Code Online (Sandbox Code Playgroud)

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

Object reference not set to an instance of an object.

更新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
Run Code Online (Sandbox Code Playgroud)

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

希望这可以为使其发挥作用提供更多见解。请帮忙 :)

Mik*_*ail 1

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

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