我有以下用户控件(现在,更多将进入相同的命名空间)
Ortund.Project.UserControls.FooControl
Ortund.Project.UserControls.BarControl
我已经在我的web.config中正确引用了这些(我知道我已经让这些控件在页面上正常工作了。)
<pages>
<controls>
<add tagPrefix="ortund" tagName="Foo" src="~/UserControls/FooControl.ascx" />
<add tagPrefix="ortund" tagName="Bar" src="~/UserControls/BarControl.ascx" />
</controls>
</pages>
这允许我在我的标记中使用控件,如下所示:
<div>
<ortund:Foo runat="server" ID="oFoo" />
<ortund:Bar runat="server" ID="oBar" />
</div>
至少,如果页面的Designer文件在上述命名空间中正确引用它们,那就是它。
相反,我在设计器文件中找到的是:
/// <summary>
/// oFoo control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.UserControl oFoo;
/// <summary>
/// oBar control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.UserControl oBar;
现在,我可以进入并更改它,但由于该文件是自动生成的,因此无需再次改变,因为没有明显的原因。因此,我无法访问用户控件中的任何公共字段,这是功能的主要问题。
那么,为什么我的设计师文件如此肆无忌惮,如此愚蠢错误呢?我该如何解决?
答案 0 :(得分:1)
根据我对UserControl
行为不端的经验,通常是因为你的控件的命名空间有一些“怪异”,或者有一个Visual Studio无法理解的继承层次结构,或者,你正在web.config
做一些低于应用程序级别的事情(尽管这通常导致命名空间的位被切断,而.designer.cs拒绝编译)。
我刚刚在上面创建了一个“简单”的复制品,设计师将声明正确地表达为:
protected global::Ortund.Project.UserControls.FooControl oFoo;
您拥有的一个选项是对声明上方的注释(“将设计器文件中的移动字段声明修改为代码隐藏文件。”)进行操作,并将声明从.designer.cs文件移动到.cs中文件。这个可能阻止设计师重新生成一个不正确的声明,尽管它已经这样做的事实可能意味着有足够的错误的它会继续这样做,尽管你的最好的努力。
有关您的UserControl和页面的解决方案/项目结构和继承层次结构(如果有)的更多信息可能有助于缩小范围!