Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same code behind file for multiple .aspx pages

Tags:

c#

.net

asp.net

We are developing a site inside a CMS that pushed files to our production server. If the .apsx pages created share the same code behind file, will this cause a problem?

like image 856
Alex Avatar asked Aug 13 '11 06:08

Alex


People also ask

Is it possible for multiple ASPX pages to use the same code?

Yes. It is technically possible, but it is not a supported way of using ASP.NET and there will likely be gnarly difficulties with it. You should use User Controls or AppCode instead. Save this answer.

How do you relate an ASPX page with its code behind page?

aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The . aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.

What will be an extension of code behind page of ASPX?

Code Behind refers to the code for an ASP.NET Web page that is written in a separate class file that can have the extension of . aspx. cs or . aspx.


3 Answers

Why don't you let both pages inherit from 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
}
like image 195
Bazzz Avatar answered Oct 22 '22 06:10

Bazzz


Yes. It is technically possible, but it is not a supported way of using ASP.NET and there will likely be gnarly difficulties with it.

You should use User Controls or AppCode instead.

like image 40
Peter Olson Avatar answered Oct 22 '22 04:10

Peter Olson


I would suggest to avoid such design, so each page should has own code behind file. Also take a look at the following ASP.NET features whcih can simplify sharing of common layout and behaviour across the web pages:

  • Master pages (.master), basically multiple pages which has the same layout could use a shared master page which provides the common layout and supporting code behind logic. Moreover - You can switch master pages in runtime and master pages could be nested, so Master Page could be placed inside of an other Master Page, this gives you a much of freedom and flexibility to design your web application layout and separate responsibilities as well.
  • User Controls (.ascx), it worth to separate concerns and split page by a set of controls, for instance, it could be LoginControl.ascx,, AdControl.ascx, SearhcControl.ascx, so you keep a code of page clean and each control provides specific functionality and own layout.
  • Inheritance, so basically set of pages have the same base class which inherited from the Page class (I would not suggest use this approach massively, one base page it is enough)

    You may use other common development techniques like dependency injection to share code across multiple pages, this depends on particular case and business goals which were considered under the hood.

like image 26
sll Avatar answered Oct 22 '22 06:10

sll