经典的 ASP.NET 是否可以动态加载已声明的 WebControl 的子类型

发布于 2025-01-02 17:48:03 字数 680 浏览 0 评论 0原文

我希望能够以标准方式以声明方式指定任何给定页面的 Web 控件,但是运行时不一定是我指定的类型的新实例,而是例如检查 web.config是否应使用不同的 Web 控件(将从指定的 Web 控件继承)。

例如,我可以在路径 /templates/controls/default/PersonSelector.ascx 中有一个 webcontrol,在路径 /templates/controls/CUSTOMER_A/PersonSelector.ascx 中有另一个 webcontrol(从第一个继承),

所以如果我的配置表明我更喜欢从可用的 CUSTOMER_A 文件夹加载控件,并且由于在这种情况下可用,因此用于页面的实际标记将出现来自.../CUSTOMER_A/PersonSelector.ascx。编译时假设它实际上是 ../default/PersonSelector.ascx 的实例的所有代码仍然可以工作,因为它实际上是一个子类。

据我所知,这对我们的网站来说是一个重大好处,因为我们的网站是一个多租户网站,其中 99.5% 的生成 html 对于所有客户来说都是相同的(自然除了换肤之外),但一些客户想要不同的页眉/页脚/选择逻辑等。到目前为止,所有差异都是通过膨胀控件和各处的 if/else 来处理的。

我意识到 asp.net 可能不是做这种事情的首选武器,但它是我们拥有的武器,而且代码库足够大,重写会很痛苦。

I want to be able to declaratively specify the webcontrols of any given page in the standard way, but have the runtime not necessarily new up instances of the types I specified, but rather for instance check against web.config whether a different webcontrol (which would inherit from the specified one) should be used instead.

for instance I could have a webcontrol in the path /templates/controls/default/PersonSelector.ascx and another one in the path /templates/controls/CUSTOMER_A/PersonSelector.ascx (inheriting from the first)

so if my config indicated that I preferred to load controls from the CUSTOMER_A folder where available, and since it would be available in this case, the actual markup which was used for the page would come from .../CUSTOMER_A/PersonSelector.ascx. All code which compile-time assumed that it was in fact an instance of ../default/PersonSelector.ascx would still work since it is actually a subclass.

This would afaik be a major benefit to our site which is a multitenant site in which 99.5% of the generated html is identical across all our customers (except for skinning, naturally) but some of the customers wants different headers/footers/selection-logic etc. So far all the differences are handled by bloating the controls and if/else-ing all over the place.

I realize that asp.net is probably not the weapon of choice for doing this kind of thing, but it's the one we've got, and the codebase is large enough that a rewrite would be a pain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最舍不得你 2025-01-09 17:48:03

是的,您可以使用 < 动态加载用户控件代码>LoadControl方法。假设您有 Control1Control2,其中 Control2 继承自 Control1,您可以在页面中执行此操作:

//Obtain control name in the format "~/pathFromApplicationRoot/ControlName.ascx"
string controlName = MethodThatDecidesTheControlNameToLoad();
//Load control
var userControl = (Control1)this.LoadControl(controlName);
//Add it to page
AControlSuchAsAPlaceHolder.Controls.Add(userControl);

编辑:
为了能够在标记中以声明方式指定用户控件,我想如果配置告诉它要覆盖,您可以使用户控件用租户特定的控件替换其自己的内容。

Yes, you can load user controls dynamically using the LoadControl method. Provided that you have Control1 and a Control2, where Control2 inherits from Control1, you could do this in a page:

//Obtain control name in the format "~/pathFromApplicationRoot/ControlName.ascx"
string controlName = MethodThatDecidesTheControlNameToLoad();
//Load control
var userControl = (Control1)this.LoadControl(controlName);
//Add it to page
AControlSuchAsAPlaceHolder.Controls.Add(userControl);

Edit:
In order to be able to specify your user controls declaratively in markup I guess you could make the user control replace its own content with the tenant specific control if the config told it to override.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文