【问题标题】:ASP.NET Web Forms: Use same tagPrefix for web controls from different namespacesASP.NET Web 窗体:对来自不同命名空间的 Web 控件使用相同的 tagPrefix
【发布时间】:2017-12-03 04:25:00
【问题描述】:

我在Web.config 中有registered two namespaces,就像这样:

<add tagPrefix="a" assembly="WebApplication1" namespace="WebFormsApplication1.Controls1" />
<add tagPrefix="a" assembly="WebApplication1" namespace="WebFormsApplication1.Controls2" />

我在.aspx 页面中这样使用它们:

<a:WebCustomControl1 ID="Control1" runat="server"></a:WebCustomControl1>
<a:WebCustomControl2 ID="Control2" runat="server"></a:WebCustomControl2>

这是 VS 为该页面生成的(损坏的).designer.cs 文件(请注意,它对两个控件都使用 Controls2 命名空间,尽管 Control1 位于 Controls1 命名空间中):

protected global::WebFormsApplication1.Controls2.WebCustomControl1 Control1;
protected global::WebFormsApplication1.Controls2.WebCustomControl2 Control2;

看起来 Web.config 中的第二个 &lt;add 正在覆盖第一个。 我希望它追加而不是覆盖,这可能吗?


我想还有其他选择:

  1. 将所有内容放在一个命名空间中
  2. 为每个命名空间使用唯一的tagPrefix

但它们并不酷,因为我们这里有很多控件。

请注意,如果在 aspx 本身中注册命名空间,它将正常工作:

<%@ Register TagPrefix="a" Namespace="WebFormsApplication1.Controls2" Assembly="WebFormsApplication1" %>
<%@ Register TagPrefix="a" Namespace="WebFormsApplication1.Controls" Assembly="WebFormsApplication1" %>

所以它看起来像Web.config 解析中的一些错误。

(我在 VS 2017 上,但他们告诉我至少从 VS 2010 开始)。

【问题讨论】:

  • 不要。由于命名空间阴影,它可能会导致不可预知的结果。

标签: c# asp.net visual-studio webforms


【解决方案1】:

如果我过去需要这个,我已经删除了设计器文件并在后面的代码中手动声明了所有控件。

也可能有一种方法(但我不记得具体如何)告诉设计器不要在设计器文件中生成那些特定的控件。然后你在后面的代码中手动添加它们。

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2012-03-07
    • 2015-04-12
    • 1970-01-01
    • 2016-09-16
    • 2022-01-26
    相关资源
    最近更新 更多