New & confused-Possible to inherit aspx forms?

J

Jeff

Greetings
I am beginning a web app. I am using C# aspx forms for my web pages.
Is it possible to create/use one aspx file as a base from which to
derive other aspx files? I can not get this to work.
What I tried was this:
Create a new web project.
- rename WebForm1.aspx to MyBase.aspx
- Add (grad from tool box) a label to the html form
- Then create another aspx file (using "Add Web Form"). This produces
WebForm2.aspx.
- Then, I tried several ways of getting WebForm2.aspx to inherit or
appear like MyBase
1. In the WebForm2.aspx.cs file, I changed the class declaration
to:
Public Class WebForm2 : MyBase.aspx{
........
}
No good.
2. In the html view I changed the first line parameters to:
Codebehind="MyBase.aspx.cs"
&
Inherits="MyBaseNameSpace.MyBase.aspx"
No good.
3. A combination of both of the above also does not work

No Matter what I do, I can get the WebForm2 page to load, but it does
not display the label that I have on the base page.

I find lots of references too and documentation about inheriting
windows forms, but nothing on web pages(forms)
Can this be done somehow?

Thanks
Jeff
 
C

carion1

Take a look at user controls. I think that may be the answer to what you
are really trying to do.
 
T

Tasos Vogiatzoglou

Try to understand what you are doing.

1) You create an ASPX page (an HTML file that will be processed by the
ASP.NET framework) and you put a label on it by dragging it from the
toolbox.
2) In your codebehind you can see a declaration of this component.

Try to delete the CodeBehing and the codebehind and inherits tags in
the Page directive on your aspx.

Compile and check the result.

The label is still there.

This can show you that the component declaration and placement is
happening somewhere else, in fact, it is happening inside the class
that is compiled from your ASPX page. When you ask for an ASPX page,
the ASP.NET framework compiles a dynamic class from your aspx page,
that inherits from the code-behind file (that's why protected
declarations and not private). Theoritically you could put code and
components in the same aspx file and everything should work.

Now, you take the codebehind, which has NO information about the
components registration and placement and you derive a page from it.
As you can understand, this will not work.

In order to have this kind of "visual inheritance" you must put the
registration/placement logic in the class that you inherit, in our case
the code behind class.

I hope that this will clarify the situation. You should also check this
:
ms-help://MS.MSDNQTR.2005APR.1033/vbcon/html/vbconWebFormsCodeModel.htm
 
J

Jeff

Tasos

I looked at the help reference that you suggested. They basically
describe placing code (scripts) in the html file, if I want to use
only one file instead of two. I understand that, I have seen classic
ASP.

Now, if I understand this, you are proposing a different approach:
In order to have this kind of "visual inheritance" you must put the
registration/placement logic in the class that you inherit, in our case
the code behind class.
Can I write the html/ASP (registration/placement logic ) from within a
code behind file and get it to display?

If I could do this, then I could set up a base page (aspx.cs) for my
repetitive labels, etc, and then refer subsequent aspx.cs pages to
inherit from this, rather than from System.Web.UI.Page.
Is that correct?

Thanks
Jeff
 
T

Tasos Vogiatzoglou

The reference I sent you was the WebForms code model and it describes
how webforms work.

Yes ... You could add initialization logic to a Page derived class and
inherit this behaviour to your own pages.

Keep in mind that this way you wont have the advantages of the
designer. Maybe you should consider some other alternative if you have
complicated UI inheritance.
 
J

Jeff

Tasos
Perhaps I will scrap the page inheritance idea for now.
It appears more trouble that I thought it was worth.

Thanks alot
Jeff
 

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

Top