Adding attribute to html tag via code-behind results in error type (System.Web.UI.HtmlControls.HtmlElement) is not compatible

不想你离开。 提交于 2019-12-10 16:38:43

问题


When I try to add an attribute to my html tag in my masterpage:

<!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">

like so in code behind:

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

I get this error:

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 

I also checked main.master.designer.vb and it holds this line:

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

Now, the weirdest thing is: this code works correct on my local machine, but not on my production server. I don't know what's causing this different behavior either.

I also looked at this post: set pages main html tag in c#

UPDATE When my situation is as follows:

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

commented this line:

'primtag.Attributes.Add

I get:

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>

After that I tried changing that line in the main.master.designer.vb file from Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlElement to Protected WithEvents primtag As Global.System.Web.UI.HtmlControls.HtmlGenericControl On the production server I get:

Object reference not set to an instance of an object.

UPDATE 2

It may have something to do with compilation of my solution, I also checked these posts:

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

I then check my IIS7.5 setting on local machine, there the application pool was set to ASP.NET4.0, same as on my production server. My target framework compilation in Visual Studio is set to .NET Framework 4.5.

Hopefully this provides more insights in getting this to work. Please help :)


回答1:


Possibly, each dynamically added attribute should start with any special (for example, the "xml") prefix.

Check this thread, which may be helpful.



来源:https://stackoverflow.com/questions/20060196/adding-attribute-to-html-tag-via-code-behind-results-in-error-type-system-web-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!