Using the same code per file for multiple .aspx pages

We are developing a site inside the CMS that places files on our production server. If the generated .apsx pages have the same code behind the file, will this cause a problem?

+4
source share
3 answers

Why don't you let both pages inherit the same class?

 public class MyPage : System.Web.UI.Page { // common logic that both pages should have protected void Page_Load(object sender, EventArgs e) { } } public partial class PageA : MyPage { // specific logic for page A } public partial class PageB : MyPage { // specific logic for page B } 
+8
source

Yes This is technically possible, but it is not a supported way to use ASP.NET, and most likely there will be formidable difficulties with it.

Instead, use User Controls or AppCode .

+3
source

I would suggest avoiding such a construction, so each page should have its own code behind the file. Also consider the following ASP.NET features that can simplify sharing common layout and behavior on web pages:

  • Master pages (.master), basically several pages with the same layout, can use a common master page, which provides general layout and code-by-logic support. In addition, you can switch master pages at runtime, and master pages can be nested, so the main page can be placed inside another main page, this gives you more freedom and flexibility when developing the layout of your web application and individual responsibilities.
  • User controls (.ascx), it’s worthwhile to separate the problems and divide the page into a set of controls, for example, it can be LoginControl.ascx, AdControl.ascx, SearhcControl.ascx, so you keep the page code clean and each control provides certain functions and own layout.
  • Inheritance, therefore basically a set of pages has the same base class that is inherited from the Page class (I would not suggest using it approach massive, one base page is enough)

    You can use other common development methods, such as dependency injection, to share code on multiple pages, it depends on the specific case and business goals that were considered under the hood.

+2
source

All Articles