By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,677 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,677 IT Pros & Developers. It's quick & easy.

Inherit a code behind page?

P: 20
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
Expand|Select|Wrap|Line Numbers
  1. Public MustInherit Class DefaultCode
  2.  
  3.     Inherits System.Web.UI.Page
  4.  
Home.aspx
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Home.aspx.vb" Inherits="DefaultCode" %>
  2.  
Home.aspx.vb
Expand|Select|Wrap|Line Numbers
  1. Partial Class Home
  2.  
  3.     Inherits DefaultCode
  4.  
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.
Mar 28 '08 #1
Share this Question
Share on Google+
5 Replies


nateraaaa
Expert 100+
P: 663
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
Mar 28 '08 #2

malav123
100+
P: 217
Ya it's true user controlls are very useful and best way to accomplish you desired task.....
Mar 28 '08 #3

balabaster
Expert 100+
P: 797
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
Mar 28 '08 #4

P: 20
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.
Mar 28 '08 #5

balabaster
Expert 100+
P: 797
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.
Mar 28 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.