Page Inheritance

J

Jonathan Wood

Okay, I searched dozens of Websites tonight for information on page
inheritance. Every single bit of information I found was talking about
code-behind scripting.

What if I don't use code-behind? Does that mean page inheritance is ruled
out? For that matter, how do I have know what the class name is for a page I
create without code behind?

I have an existing page and would like to create a second one that is almost
identical but not quite. Page inheritance would be cool here, but at this
point I don't know if this is possible.

Thanks.

Jonathan
 
G

George

Page inheritance is a good thing. I am using it in any of my projects.
But i am afraid, you misunderstand what you want.
-----------------
First to answer your question.
You can specify class name using
<%@ Page ClassName="MyClass" %>
and use that name.

--------------------------------

It does not make sence to inherit from page that has HTML. I do not see how
would your HTML mix if let say Page1 has HTML and Page2 has HTML and Page2
inherits from Page1.

You inherit the common method and properties. But not HTML.
For that you need to create page clsStandardPage in APP_CODE folder which
inherits System.UI.Web.Page
And your pages inherit clsStandardPage
<%@ Page Language="C#" Inherits="clsStandardPage"%>

I guess you could create clsStandardPage as aspx page and give it a
ClassName ='clsStandardPage' and inherit from it. But that would be
overkill. Since there is parsing involved. So better create it as a class in
APP_CODE.
---------------------------------------

If you want to reuse HTML on pages then use MasterPage or UserControls.



George.
 
J

Jonathan Wood

Page inheritance is a good thing. I am using it in any of my projects.
But i am afraid, you misunderstand what you want.

Actually, I know exactly what I want. What I'm having trouble with is what
ASP.NET can do. :)
First to answer your question.
You can specify class name using
<%@ Page ClassName="MyClass" %>
and use that name.
Cool.

It does not make sence to inherit from page that has HTML. I do not see
how would your HTML mix if let say Page1 has HTML and Page2 has HTML and
Page2 inherits from Page1.

Well, I think it does make sense. A type of this inheritance occurs with
master pages. And, in fact, I want the exact same layout in my "derived"
page. It would just change the underlying SQL. But perhaps this type of
inheritance is not possible with ASP.NET.
You inherit the common method and properties. But not HTML.
For that you need to create page clsStandardPage in APP_CODE folder which
inherits System.UI.Web.Page
And your pages inherit clsStandardPage
<%@ Page Language="C#" Inherits="clsStandardPage"%>

And what kind of file would clsStandardPage be? Just a CS file that declares
a page? I think I follow what you are saying. But if this base page couldn't
handle events from any added controls, it seems like it's use is somewhat
limited.
I guess you could create clsStandardPage as aspx page and give it a
ClassName ='clsStandardPage' and inherit from it. But that would be
overkill. Since there is parsing involved. So better create it as a class
in APP_CODE.

I might play with that just to better understand what is going on. But
sounds like it won't do what I'd like.
If you want to reuse HTML on pages then use MasterPage or UserControls.

I already use Master pages. User controls are a possibility. Obviously, I'm
still learning ASP.NET.

Thanks for all the info.
 
G

George

I see what you want to do.
I guess in your case the output is the same but depends on SQL statement.

I would do it like user control. Add public property like Sql for example.

So it's like following (pseducode)
<%@ Control Language="C#" ClassName="ConfirmOrder"
Inherits="clsUserControlBase"%>
<script runat="server">
string _Sql;
public string SQL
{
get {return _sSql;}
set {_sSql = value;
}
void override Page_Load()
{
DataTable dt = GetData(_sSql);
....blablabla....
}
</script>

Then on your page you could use it like this.

<uc1:ConfirmOrder id=id1 runat=server SQL="SELECT * FROM myTable" />
You declaratively define your SQL.

Or if it's more complicated than that you can assign your SQL on Page_Init
method.

George.
 
J

Jonathan Wood

Yeah, it is more complicated than that since I'm correctly implementing
paging (instead of using the braindead default where it requests everything
from the database and then just displays whatever goes in the current page)
and am calling various object methods. I'm a little lost right now. But I'm
sure it could work, possibly with some variation on a user control as you
suggest.

BTW, I tried specifying the class name of a page (ASPX file), then put that
same name as the Inherits value in another page (ASPX file), but I got an
error something about it could not load that class. So I guess it can't work
that way at all?

Thanks.
 
J

Jonathan Wood

I hadn't--not familiar with that.

I tried adding the following line to the aspx file that contains my derived
class:

<%@ Reference Page="~/<aspx file>" %>

But I still get the error "Could not load type '<class name>'".

Is there some combination I'm missing?

Thanks.

Jonathan
 
G

George

Can you show me those 2 pages... strip irrelevant HTML.
Just leave the @Page from BOTH pages, @Reference tags and line of code where
you using the class name.

George
 
J

Jonathan Wood

Hi George,
Can you show me those 2 pages... strip irrelevant HTML.
Just leave the @Page from BOTH pages, @Reference tags and line of code
where you using the class name.

Sure. At this point, there is no code referencing the class name. I'm just
trying to see if I can make one page look like another by deriving its
content. I could play with things from there.

===== [[ Listings.aspx ]] =====

<%@ Page Title="SoundFilesOnline" Language="C#"
MasterPageFile="~/Default.master" ClassName="ListingsPage" %>
<%@ OutputCache Duration="60" VaryByParam="cat;page" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="SoftCircuits" %>

<script runat="server">

</script>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

</asp:Content>

===== [[ Default2.aspx ]] =====
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master"
Inherits="ListingsPage" %>
<%@ Reference Page="~/Listings.aspx" %>

<script runat="server">

</script>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

</asp:Content>

Thanks.

Jonathan
 
G

George

I see now... I apologize for misleading you.
I am afraid you can not use it in Inherits property.

The reason is that because @Page directive is processed first, before
@Reference directive.
So you can use type ListingsPage anywhere in the code (using the
@Reference). But apparently can not in the Inherits property.

-----------------------------
But again I would suggest to rethink your approach. I honestly do not see
what will you achieve by Inheriting HTML from another page.
Your best bet is to use UserContols for that. And if you only need to
inherit functionality then create separate class.

George.







Jonathan Wood said:
Hi George,
Can you show me those 2 pages... strip irrelevant HTML.
Just leave the @Page from BOTH pages, @Reference tags and line of code
where you using the class name.

Sure. At this point, there is no code referencing the class name. I'm just
trying to see if I can make one page look like another by deriving its
content. I could play with things from there.

===== [[ Listings.aspx ]] =====

<%@ Page Title="SoundFilesOnline" Language="C#"
MasterPageFile="~/Default.master" ClassName="ListingsPage" %>
<%@ OutputCache Duration="60" VaryByParam="cat;page" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="SoftCircuits" %>

<script runat="server">

</script>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

</asp:Content>

===== [[ Default2.aspx ]] =====
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master"
Inherits="ListingsPage" %>
<%@ Reference Page="~/Listings.aspx" %>

<script runat="server">

</script>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">

</asp:Content>

Thanks.

Jonathan
 
J

Jonathan Wood

I see now... I apologize for misleading you.
I am afraid you can not use it in Inherits property.

The reason is that because @Page directive is processed first, before
@Reference directive.
So you can use type ListingsPage anywhere in the code (using the
@Reference). But apparently can not in the Inherits property.

So, then, I can't have one aspx page inherit visual components of another
aspx page? I'll have to research this. I mean, what is Inherits even for?
But again I would suggest to rethink your approach. I honestly do not see
what will you achieve by Inheriting HTML from another page.
Your best bet is to use UserContols for that. And if you only need to
inherit functionality then create separate class.

Yeah, this has turned mostly into an academic discussion. I've used
inheritance in C++ code many, many times. And I just wasn't sure what was
available with aspx files.

I'm actually leaning to simply, in the page's load event, reprogram the
ListView control to call different methods based on query strings. If that
works, it seems like the simplest approach.

Thanks.

Jonathan
 
G

George

You can, but you need to use CodeBehind model for that.
So if your ListingsPage page had a code behind with class name ListingsPage.
Then you could use the Inherits. Since the code behind is compiled into DLL
first and at the time @Page directive is compiled this type is known.

Just test it with following
--------------ListingsPage1.aspx------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="ListingsPage1.aspx.cs" Inherits="WebApplication1.ListingsPage1"
%>

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<%= this.GetType().FullName %>
</asp:Content>

----------ListingsPage1.aspx.cs
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class ListingsPage1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}
}
---------------------------Default.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master"
Inherits="WebApplication1.ListingsPage1"%>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1"
Runat="Server">
<%= this.GetType().FullName %>
</asp:Content>

-------------------

And it will work.


George.
 
J

Jonathan Wood

I'll save your email for when I get more time to play with this.

For now, I found I was able to make my page (mostly a ListView and DataPager
control) work in both modes using surprisingly little code.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top