I've got a site with two master pages: one for one-column layout and one for two-column layout. These are nested within a master page that provides a common header and footer.
我有一个包含两个母版页的网站:一个用于一列布局,一个用于两列布局。它们嵌套在提供公共页眉和页脚的母版页中。
I have functionality that I want each of the content pages to have; should I:
我有我希望每个内容页面都具有的功能;我是不是该:
- create a page base class and inherit that inside my content pages, or
- create a master page base class and inherit that inside one of my levels of nested master page?
创建一个页面基类并在我的内容页面中继承它,或者
创建一个母版页基类并在我的一个嵌套母版页中继承它?
Ultimately I want the content pages to have access to a connection object and to a configuration object that I want to be instantiated as each page loads.
最终,我希望内容页面能够访问连接对象和我希望在每个页面加载时实例化的配置对象。
3 个解决方案
#1
Master pages should only be used for layouts in my opinion.
在我看来,母版页只应用于布局。
If you want to be doing work such as creating connections, do that in a base class.
如果您想要创建连接等工作,请在基类中执行此操作。
However, it is important that you not create a database connection at the start of your page, and close it at the end. You should be opening and closing connections as you run individual queries. This allows connection pooling to work efficiently.
但是,重要的是不要在页面的开头创建数据库连接,并在结束时关闭它。您应该在运行单个查询时打开和关闭连接。这允许连接池有效地工作。
Additionally, I would not be putting a connection of any type in the page itself, as you want to separate your functionality as much as possible from the layout.
另外,我不会在页面本身中添加任何类型的连接,因为您希望尽可能地将您的功能与布局分开。
#2
Ultimately I want the content pages to have access to a connection object and to a configuration object that I want to be instantiated as each page loads.
最终,我希望内容页面能够访问连接对象和我希望在每个页面加载时实例化的配置对象。
Base page is a prime target for this.
基页是此的主要目标。
#3
I typically use a base page that contains a number of features that ease the creation of content pages. See http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspx for some examples of useful BasePage features.
我通常使用一个基页,其中包含许多可以简化内容页面创建的功能。有关有用的BasePage功能的一些示例,请参阅http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspx。
#1
Master pages should only be used for layouts in my opinion.
在我看来,母版页只应用于布局。
If you want to be doing work such as creating connections, do that in a base class.
如果您想要创建连接等工作,请在基类中执行此操作。
However, it is important that you not create a database connection at the start of your page, and close it at the end. You should be opening and closing connections as you run individual queries. This allows connection pooling to work efficiently.
但是,重要的是不要在页面的开头创建数据库连接,并在结束时关闭它。您应该在运行单个查询时打开和关闭连接。这允许连接池有效地工作。
Additionally, I would not be putting a connection of any type in the page itself, as you want to separate your functionality as much as possible from the layout.
另外,我不会在页面本身中添加任何类型的连接,因为您希望尽可能地将您的功能与布局分开。
#2
Ultimately I want the content pages to have access to a connection object and to a configuration object that I want to be instantiated as each page loads.
最终,我希望内容页面能够访问连接对象和我希望在每个页面加载时实例化的配置对象。
Base page is a prime target for this.
基页是此的主要目标。
#3
I typically use a base page that contains a number of features that ease the creation of content pages. See http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspx for some examples of useful BasePage features.
我通常使用一个基页,其中包含许多可以简化内容页面创建的功能。有关有用的BasePage功能的一些示例,请参阅http://dotnetslackers.com/articles/aspnet/Four-Helpful-Features-to-Add-to-Your-Base-Page-Class.aspx。