Newbie Question: Moving code to external file

S

Simon Harris

Hi All,

I have a page that includes a database connection, recordset (Binded to a
data grid) and a small string related function.

As much as possible, I like to write modular code, which can be reused. Can
someone please point me in the write direction for moving my 'common' code
to an external file - Rather than just having it in a script block at the
top of each page.

Thanks loads.

Simon.

--
-
* Please reply to group for the benefit of all
* Found the answer to your own question? Post it!
* Get a useful reply to one of your posts?...post an answer to another one
* Search first, post later : http://www.google.co.uk/groups
* Want my email address? Ask me in a post...Cos2MuchSpamMakesUFat!
 
P

Pete Beech

Hi Simon,
Codebehind classes (which I explained in reply to your other question)
relate to each individual page. If you have code which you want to reuse
amongst several pages, you're free to create your own classes with this code
in it, and then use it from each page where it is required (in the
codebehind, or even in the script block).

If you just include this class in the webproject, it will get included in
the codebehind assembly (roughly equivalent to a .dll) for the webproject.
(This is the dll thats sits in the bin subfolder, if you use codebehind). If
you have a lot of stuff which is reusable, you could also move it into a
separate assembly (dll), using, in VS.NET, a Class Library project. For
example, you could have a data access layer dll, which is responsible for
all data access, or you could put middle tier 'business' objects into their
own separate dll.

If you find you have common elements on each ASPX page (like some header
HTML, or a menu of links), you could start keeping that in one place by
using User controls. And, if you ever find you have certain complicated user
interface behaviour which is fairly self contained and appears on more than
one page, you might even consider writing your own custom ASP.NET webcontrol
to encapsulate this. (For example, a javascript-based Menu control, or a
toolbar control could be written as custom web controls. User controls can
also be used for simpler functionality, but are more suited for capturing
common code for just one project, whereas with custom web controls you have
much more control, and they can generally be used on many webapp projects).

Here are some good links..

http://samples.gotdotnet.com/quickstart/aspplus/doc/businessobjs.aspx
http://samples.gotdotnet.com/quickstart/aspplus/doc/webctrlauthoring.aspx
http://samples.gotdotnet.com/quickstart/aspplus/doc/webpagelets.aspx

HTH,
Cheers,
Pete Beech
 
S

Simon Harris

Thanks Peter - A very comprehensive reply, I think I'll be making heavy use
of such methods in months to come! :)

Simon.
 

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,770
Messages
2,569,586
Members
45,086
Latest member
ChelseaAmi

Latest Threads

Top