ASPX页面可以共享隐藏文件code? [英] Can ASPX pages share code behind file?

查看:130
本文介绍了ASPX页面可以共享隐藏文件code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说,我有具有相同的服务器端控件中两个ASPX页面。

Let say I have two ASPX pages with identical server-side controls.

~/TemplateA/Contact.aspx
~/TemplateB/Contact.aspx

在默认情况下,它们映射到它们各自的code隐藏文件。

By default, they are mapped to their respective code-behind file.

~/TemplateA/Contact.aspx.cs
~/TemplateB/Contact.aspx.cs

相反,我想它们指向一个单一的,共同的code-隐藏文件,如

Instead, I would like them to point to a single, common code-behind file, as

~/Classes/Contact.aspx.cs

我已经让他们从基地联系人功能类,谁暴露了整个业务逻辑继承,但我还是不明白如何引用的页面的服务器端控件到基地联系级;为例,我希望能够写:

I already have them inheriting from a base "Contact" function class, who exposes the whole business-logic, but I still can't figure how to refer the server-side controls of the pages into the base "Contact" class; as example, I would like to be able to write:

this.textBox1.Text = "Hello"

在我的基类,而不是在每个单独的cs文件写它。
显然,textBox1的是不是在我的基类已知的对象,因为它是不附加任何设计师的文件。

in my base class instead of writing it in each single .cs file. Obviously, "textBox1" isn't a known object in my base class as it has no designer file attached.

这可能吗?

推荐答案

所以在这里它是非常适合我的需求(再次感谢ps2goat的提示)解决方案。

So here it is the solution that perfectly suited my needs (thanks again ps2goat for the hint).

我的两页基本结构是:

[namespace A]
Page.aspx 
Page.aspx.cs
Page.aspx.designer.cs

[namespace B]
Page.aspx 
Page.aspx.cs
Page.aspx.designer.cs

(假设我有远远超过2页)

(assume i do have far more than 2 pages)

我需要删除的.cs和文件了.Designer.cs同时能够指在.aspx页面中声明的服务器控件。

I did need to remove the .cs and .designer.cs files while being able to refer to server controls declared in the .aspx page.

这可能是不可能从基类继承的标准,也没有使用母版页:他们的工作很好,但完全不知道孩子的ASPX服务器控件

This could not be possible with standard inheriting from base classes, nor using master pages: they work well, but are completely unaware of the children ASPX server controls.

所以,我创建了一个泛型类文件

So, I created a generic class file

[namespace COMMON]
Page.cs

在这个文件中,我都抄的.cs部分类和了.Designer.cs局部类(显然照顾改变命名的新一)的要么是原始的命名空间的内容(他们是相同的在code)。

In this file, I copied the content of both ".cs" partial class and ".designer.cs" partial class (obviously taking care of changing namespace to the new one) of either of the original namespaces (they were identical in code).

在page.aspx文件,codebehind映射从Page.aspx.cs和A.Page命名为Page.cs和Common.Page命名空间。

In page.aspx file, codebehind mapping was updated from "Page.aspx.cs" and "A.Page" namespace to "Page.cs" and "Common.Page" namespace.

因此​​,文件从这些变化:

So, files changed from these:

[Page.aspx](每个命名空间一个实例)

[Page.aspx] (one instance for each namespace)

<%@ Page Title="Page" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="Page.aspx.cs" Inherits="Project.A.Page" %>
<asp:Content ID="ContentB" ContentPlaceHolderID="cBody" runat="server">
    <asp:TextBox ID="txbTest" runat="server" MaxLength="75"></asp:TextBox>
</asp:Content>

[Page.aspx.cs](每个命名空间一个实例)

[Page.aspx.cs] (one instance for each namespace)

namespace Project.A
{
    public partial class Page: BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txbTest.Text = "Hello";
        }
    }
}

[Page.aspx.designer.cs](自动生成的,每个命名空间一个实例)

[Page.aspx.designer.cs] (auto-generated, one instance for each namespace)

namespace Project.A {
    public partial class Page{
        protected global::System.Web.UI.WebControls.TextBox txbTest;                
    }
}

要这些:

[Page.aspx](每个命名空间一个实例)

[Page.aspx] (one instance for each namespace)

<%@ Page Title="Page" Language="C#" MasterPageFile="~/Page.Master" AutoEventWireup="true" CodeBehind="Page.cs" Inherits="Project.COMMON.Page" %>
<asp:Content ID="ContentB" ContentPlaceHolderID="cBody" runat="server">
    <asp:TextBox ID="txbTest" runat="server" MaxLength="75"></asp:TextBox>
</asp:Content>

[Page.cs](一个奇异例如,通过老的.cs和.designer文件的内容。制造)

[Page.cs] (one SINGULAR instance, made by the content of old .cs and .designer files)

namespace Project.COMMON
{
    public partial class Page: BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.txbTest.Text = "Hello";
        }
    }

    public partial class Page{
        protected global::System.Web.UI.WebControls.TextBox txbTest;                
    }
}

然后我删除的页面的的.cs和文件了.Designer.cs每个实例,留下一个结构,我需要的,如:

Then I deleted each instance of page's .cs and .designer.cs files, leaving a structure as I needed, like:

~/A/Page.aspx
~/B/Page.aspx
~/COMMON/Page.cs

和它的工作原理就像一个魅力!

And it works like a charm!

所以,最后,我可以说,ASPX还是在投影的网络软件,这个项目的MVC移植可以等待一个选择。

So, finally, I can say that aspx is still a choice when projecting web software, MVC porting of this project can wait.

这篇关于ASPX页面可以共享隐藏文件code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆