Using Codebehind

N

nevets2001uk

Hi. I've just started my second ASP.NET (VB) app and I'm using
codebehind this time.

I'm not using visual studio but am instead coding it all in notepad
(HTML, ASP.NET and CSS)

I'm trying to understand how to best use codebehind to do what I want
to achieve. Basically, so far I have a codebehind file for one page
which works as I wanted but I want other pages to share that
codebehind file. That's no problem except I want each one to use
there own Page_Load subroutine in the codebehind and to share some
sub's.

The only way I think might work would be to use different classes
within the codebehind file. I did consider two codebehind files I
couldn't inherit them both. I'm not sure how the classes work exactly
and I have no idea if I can pass variables between them and call subs
from one class into another!

I'm really after some information on how to use the classes /
codebehind to allow me to have page specific subroutines and shared
subroutines within one or more codebehind file.

Any advice welcomed to help me learn this a bit better will be
appriciated

Thanks,

Steve
 
K

Kevin Spencer

You need to think object-oriented here, and I think you would benefit from a
bit of study regarding object-oriented programming, particularly with
regards to inheritance. In classic ASP, you would think of files. In
ASP.Net, you work with files at design-time for writing code, but it is best
not to think about the files, but rather, to think about classes. An ASP.Net
Page is derived from combining a Page class definition (CodeBehind) with a
Page Template that inherits that class. By inherits, I mean that it has all
the properties, methods, fields, event handlers, etc., that the CodeBehind
class has, as well as the pieces of the Page class that are defined via the
Template code. This is an important distinction. So, rather than referring
to the "CodeBehind file" you will find it much easier to conceive of this by
referring to it as the "CodeBehind Class." When you think of it in this way,
you can see that putting 2 different Page class definitions in one
CodeBehind file accomplishes nothing, as they are still 2 distinct classes,
and share nothing in common.

Any number of Page Templates can inherit the same CodeBehind class. However,
it gets a bit tricky because of the separation of the 2 pieces. For example,
if you include any Server Controls in the Page Template, and you want to be
able to manipulate them programmatically in the CodeBehind class, there
needs to be a declaration of the Control's class in the CodeBehind.

In fact, you didn't state what your business requirements are, but I suspect
that what you want could be much better accomplished in some other way. For
example, if you have business logic that you want shared between Pages, put
it into an Assembly (DLL) which all pages can use. If you have some layout
elements that you want to share betwen pages, you can either create User
Controls that you use in all pages, or use CSS to do your layout. CSS is
very powerful, and can be used to do nearly all the layout-related
customization of a site. A good reference for using CSS in this way can be
found at http://csszengarden.com/. One advantage of using CSS is that you
can separate your layout from your interface logic very neatly in this way.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.


nevets2001uk said:
Hi. I've just started my second ASP.NET (VB) app and I'm using
codebehind this time.

I'm not using visual studio but am instead coding it all in notepad
(HTML, ASP.NET and CSS)

I'm trying to understand how to best use codebehind to do what I want
to achieve. Basically, so far I have a codebehind file for one page
which works as I wanted but I want other pages to share that
codebehind file. That's no problem except I want each one to use
there own Page_Load subroutine in the codebehind and to share some
sub's.

The only way I think might work would be to use different classes
within the codebehind file. I did consider two codebehind files I
couldn't inherit them both. I'm not sure how the classes work exactly
and I have no idea if I can pass variables between them and call subs
from one class into another!

I'm really after some information on how to use the classes /
codebehind to allow me to have page specific subroutines and shared
subroutines within one or more codebehind file.

Any advice welcomed to help me learn this a bit better will be
appriciated

Thanks,

Steve



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
N

nevets2001uk

Thanks for your help. Do you know of any good links to help me with
things such as creating business logic in DLL's etc. Until recently I
was new to ASP.NET and have taught myself the basics as I go so any
tutorials that would help me would be great.

Cheers

Steve
 
K

Kevin Spencer

That's a tough question to answer. The authoritative site is the Microsoft
MSDN web site (http://msdn.microsoft.com). The MSDN site has a number of
articles about Best Practices, Architecture, Design Patterns, etc. However,
Microsoft also runs http://www.asp.net, and there are a large number of
other sites out there with technical information, articles, and tutorials on
the subject.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

nevets2001uk said:
Thanks for your help. Do you know of any good links to help me with
things such as creating business logic in DLL's etc. Until recently I
was new to ASP.NET and have taught myself the basics as I go so any
tutorials that would help me would be great.

Cheers

Steve



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top