Inherit a code behind page?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ashlewis
    New Member
    • Mar 2008
    • 20

    Inherit a code behind page?

    Hi all,
    I have two aspx pages:
    Default.aspx with its code behind page of Default.aspx.vb
    Home.aspx with its code behind page of Home.aspx.vb

    The Default page in effect is just a template, the Home page will be the same but with some extra functionailty. I dont want to have to copy the code from the Default.aspx.vb to the Home.aspx.vb page since I assume there is a more efficient way.

    Ive tried to inherit the Default code into the Home code like so:

    Default.aspx.vb
    Code:
    Public MustInherit Class DefaultCode
    
        Inherits System.Web.UI.Page
    Home.aspx
    Code:
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Home.aspx.vb" Inherits="DefaultCode" %>
    Home.aspx.vb
    Code:
    Partial Class Home
    
        Inherits DefaultCode
    Now that just doesnt seem to be working, it doesnt seem to recognising what the DefaultCode is when im telling it to inherit it.

    If i move the Default.aspx.vb into the App_Code file (im using Visual Web Developer) Home.aspx.vb inherits DefaultPage but Home.aspx wont run, there loads of errors.

    Thanks, Ash.
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Have you thought about creating a user control that will be used on both pages? The user control would contain the common functionality of the two pages. To me this would be an easier solution.
    Nathan

    Comment

    • malav123
      New Member
      • Feb 2008
      • 217

      #3
      Ya it's true user controlls are very useful and best way to accomplish you desired task.....

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Um...am I missing something here? Wasn't the Master pages concept developed (at least in part) for this purpose? Put all your common code, stylesheet, etc etc in the Master page, when you create a new page, select the "Select master page" option and create page content...

        Ben

        Comment

        • Ashlewis
          New Member
          • Mar 2008
          • 20

          #5
          Thank you all.

          I thought masterpages were just for the HTML side of things?
          Ive never used one, ill try one out after ive tried using user controls, another thing ive never used, any idea on how they would work in my case? any help would be appreciated, thanks.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            User controls are fabulous when you want to dynamically use templates for objects on your page, when you build a user control, think of it somewhat like an active-x control or a class in that you can create properties which set/get values inside objects within that user control. The user control would hold code in its codebehind that pertains only to functions pertaining to that user control.

            My personal opinion is that for templating whole pages, this isn't really the way to go - others may or may not disagree with me but I look at master pages, pages and user controls somewhat as a hierarchy. Where each of my user controls contains one or more controls/user controls; pages hold a bunch of user controls and a master page holds a template for a bunch of pages to provide centralized management of look and feel of your site, including global methods and scripts that should be available across the whole site.

            As I said though, that's just my personal opinion, and doesn't mean that either way is more right or wrong than the other.

            Comment

            Working...