类似的母版页,最好的 DRY 方法是什么?

发布于 2024-11-16 11:15:50 字数 1301 浏览 1 评论 0原文

我有两个类似的母版页,基本上它们非常广泛,但区别在于一个使用

common content

<form id="form1" runat="server" enctype="multipart/form-data">
    common content
</form>

common content

,另一个是

common content

<dn:Form id="form1" runat="server">
    common content
</dn:Form>

common content

我想知道如何实现这一点,而不必创建两个母版页并只需更改表单标签

...我目前想到的做法是,基本上有一个包含其他内容的母版页,第二个包含内部内容的母版页,以及另外两个仅包含表单标签和其中占位符的母版页,然后动态选择一个母版页另一个。

有没有更好的方法来做到这一点或者这是正确的方法?谢谢。

更新:我不确定我当前的想法是否得到了很好的表达:

Base.master 将具有外部内容,没有继承。

Regular 和 Modified.master 将具有不同的表单标签,两者都继承自 Base.Master

Shared.master 将具有内部内容,继承自 Regular.Master,如果需要其他表单控件,则它选择另一个 master (它具有与 FormContent 相同的 ContentPlaceHolderID),动态地使用类似的内容,可能从 web.config 等读取。这样做

    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.MasterPageFile = "~/App_Shared/RegularWebForm.Master";
        this.MasterPageFile = "~/App_Shared/UrlRewritableWebForm.Master";
    }

的目标是能够使用相同的 Master同一网站的三个不同应用程序的页面 领域。

我提出的解决方案的想法是,我将这四个母版页文件放在给定的“App_Shared”文件夹中,该文件夹通过 svn:externals 从所有项目中引用,因此我不必重复代码。我的想法是,我选择 Shared.Master(这将是所有三个应用程序的功能基本母版页文件)是否使用当前应用程序中的常规表单或用户控件,并且可以通过在应用程序的 web.config。

I have two similar master pages, basically they are pretty extensive, but the difference relies in one using

common content

<form id="form1" runat="server" enctype="multipart/form-data">
    common content
</form>

common content

and the other being

common content

<dn:Form id="form1" runat="server">
    common content
</dn:Form>

common content

I was wondering how I could accomplish this, without having to create two master pages and just changing the form tags...

The way I currently though of doing this, is basically have one master page with the other content, a second one with the inner contents, and two others that just have the form tags and a placeholder inside them, and then dynamically choosing one master page over the other.

Is there a better way to do this or is this the correct way? thanks.

Update: I'm not sure my current idea is well expressed:

Base.master would have the outer contents, no inheritance.

Regular and Modified.master would have just the different form tags, both inherit from Base.Master

Shared.master would have the inner contents, inherit from Regular.Master and in case it requires the other form control, then it chooses the other master (which has the same ContentPlaceHolderID for FormContent), dynamically with something like this, maybe reading from web.config or the like

    protected void Page_PreInit(object sender, EventArgs e)
    {
        this.MasterPageFile = "~/App_Shared/RegularWebForm.Master";
        this.MasterPageFile = "~/App_Shared/UrlRewritableWebForm.Master";
    }

The goal for this is being able to use the same Master page across three different applications for one same web domain.

The idea that my solution proposed would be that I have those four master page files in a given "App_Shared" folder, which is referenced via svn:externals from all the projects, so I don't have to repeat the code. The idea would be that I choose whether Shared.Master (which would be the functional base master page file for all three applications) uses the regular form or the user control in the current application, and that choice could be made by a setting in the web.config for the application.

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

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

发布评论

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

评论(1

愿与i 2024-11-23 11:15:50

在后面的母版页代码中,您应该能够重写 OnInit (或 OnLoad 或在任意数量的其他位置)并确定何时需要多部分加密,并且当您这样做时,调用:

Attributes.Add("enctype", "multipart/form-data");

更好:

公开布尔属性:

public bool EncodeMe {get;set;};

在每个使用主控件集的表单

Master.EncodeMe = true; // or false of course

然后在母版页中使用 bool 来确定是否进行编码。

In the master page code behind, you should be able to override OnInit (or OnLoad or in any number of other places) and determine when you need multipart encryption, and when you do, call:

Attributes.Add("enctype", "multipart/form-data");

Even better:

Expose a boolean property:

public bool EncodeMe {get;set;};

In each form that uses the master control set

Master.EncodeMe = true; // or false of course

then in the master page use the bool to determine whether or not to encode.

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