简体   繁体   中英

ASP.NET Web Forms: Use same tagPrefix for web controls from different namespaces

I've registered two namespaces in Web.config like this:

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

I'm using them like this in an .aspx page:

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

Here's the (broken) .designer.cs file VS generates for that page (notice that it uses Controls2 namespace for both controls, although Control1 lays in Controls1 namespace):

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

It looks like the second <add in Web.config is overwriting the first one. I want it to append rather than overwrite, is it possible?


I guess there are other alternatives as well:

  1. Put everything in a single namespace
  2. Use an unique tagPrefix for each namespace

But they are not cool since we have about a hujilion of controls here.

Notice that if register the namespaces in aspx itself, it'll work fine:

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

So it looks like some bug in Web.config parsing.

(I'm on VS 2017, but they told me it happened since VS 2010 at least).

In cases where I needed this in the past, I had deleted the designer files and Manually declared all the controls in the code behind.

There may also be a way (but I don't recall exactly how) to tell the Designer to not generate those specific controls in the designer file. Then you manually add them in the code behind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM