基类包含字段,但类型与控件类型不兼容
基类包含字段“lbl”,但其类型 (web.App_Code.CustomLabelControl) 与控件类型 (web.App_Code.CustomLabelControl) 不兼容。
我以前用同样的方式做过很多自定义控件,但今天我遇到了这个错误。
我有一个 Web 应用程序项目,在 App_Code
目录中包含以下类,在 web.config 中为类中的控件提供 tagprefix 引用。
我现在该怎么办?
Web.Config
<system.web>
<pages>
<controls>
<add namespace="web.App_Code" tagPrefix="CControls"/>...
标记
<form id="form1" runat="server">
<div>
<CControls:CustomLabelControl runat="server" OnClickText="Welcome" ID="lbl">
</CControls:CustomLabelControl>
</div>
</form>
类文件
namespace web.App_Code
{
public class CustomLabelControl : Control, IPostBackEventHandler, IPostBackDataHandler
{
private string _onClickText;
public CustomLabelControl()
{
}
public string OnClickText
{
get { return _onClickText; }
set { _onClickText = value; }
}
public void RaisePostBackEvent(string eventArgument)
{
throw new System.NotImplementedException();
}
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
throw new System.NotImplementedException();
}
public void RaisePostDataChangedEvent()
{
throw new System.NotImplementedException();
}
}
}
我已经检查了这些资源:
The base class includes the field 'lbl', but its type (web.App_Code.CustomLabelControl) is not compatible with the type of control (web.App_Code.CustomLabelControl).
I had done many custom controls before the same way but today I ran into this error.
I have a web application project with the below class in App_Code
directory a tagprefix reference in web.config for the control in class.
What do I do now?
Web.Config
<system.web>
<pages>
<controls>
<add namespace="web.App_Code" tagPrefix="CControls"/>...
Markup
<form id="form1" runat="server">
<div>
<CControls:CustomLabelControl runat="server" OnClickText="Welcome" ID="lbl">
</CControls:CustomLabelControl>
</div>
</form>
Class File
namespace web.App_Code
{
public class CustomLabelControl : Control, IPostBackEventHandler, IPostBackDataHandler
{
private string _onClickText;
public CustomLabelControl()
{
}
public string OnClickText
{
get { return _onClickText; }
set { _onClickText = value; }
}
public void RaisePostBackEvent(string eventArgument)
{
throw new System.NotImplementedException();
}
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
throw new System.NotImplementedException();
}
public void RaisePostDataChangedEvent()
{
throw new System.NotImplementedException();
}
}
}
I've checked these resources already:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也尝试指定程序集名称:
为了清楚起见,我会考虑为自定义控件创建专用的命名空间。也许类似于 web.App_Code.CustomControls :
Try specifying the assembly name too:
I would consider creating a dedicated namespace for your custom controls, just for the sake of clarity. Maybe something like
web.App_Code.CustomControls
:取消选中构建(和发布)选项“允许此预编译站点可更新”
它可能不足以运行时,因此检查选项“使用固定命名和单页程序集”,它解决了我的情况:)
这是一个有用的有关此错误的链接:http://forums.asp.net/t/960707.aspx
Unckeck the build (and publish) option "Allow this precompiled site to be updatable"
It might not be enought for the runtime, so check the option "use fixed naming and single page assemblies" and it solved my case :)
here is a useful link about this error: http://forums.asp.net/t/960707.aspx
请参阅 web.conf 中的 ascx 用户控件,而不是像这样的 aspx 页面:
这解决了我的问题。
Refer the ascx user control in the web.conf instead of the aspx page like this:
That solved my issue.
我知道这已经很旧了,但我在尝试将文本框更改为标签控件时遇到了同样的错误。我只是将控件的 ID 从“Insulin”更改为“Insulin2”,从而解决了该问题。我不确定它为什么有效,但这可能是微软的一个问题。
经过更多研究后,我注意到如果我关闭了 Visual Studio 中正在处理的页面的选项卡,然后重新打开它,错误就会消失吗?我希望这对某人有帮助。
I know this is old, but I had the same error trying to change a textbox to a label control. I simply changed the ID of the control from "Insulin" to "Insulin2" which fixed the issue. I'm not sure why it worked, but that may be a question for Microsoft.
After looking into this more, I noticed if I closed the tab of the page I was working on in Visual Studio and then reopened it, the error went away all together? I hope this helps somebody.
在将 Web Forms 网站转换为 MVC Web App 并将目标框架更改为 4.6 时遇到此问题。
我的解决方案是在 web.config 中的页面元素上设置 controlRenderingCompatibilityVersion 属性:
Encountered this problem while converting a Web Forms website to MVC Web App, and changing target framework to 4.6.
The solution for me was to set the controlRenderingCompatibilityVersion attribute on the pages element in web.config:
就我而言,我的应用程序已发布,它在我的本地以及 UAT 环境中运行。问题仅出现在生产中。
所以我所做的错误是,我没有将新发布的版本 ascx 文件移至生产环境,类似地,它正在读取旧版本的发布文件。
通过将最新文件移至生产环境或替换它们,这个问题得到了解决。
In my case, my application was published one , it was working in my local as well as in UAT environment .The problem was only in production.
so the mistake what i did was, i not moved the new published version ascx files to production, similarly it was reading old version published file.
By moving the latest files to production or replacing them, this issue got resolved.