Multiple Codebehind Pages? Possible/Practical?

R

Ranginald

This question is about how to handle an .aspx page that references
multiple methods and where to store these methods (e.g. one codefile or
multiple codefiles).

PREFACE
========
I have a simple category/product asp.net/C# web site I am coding in
web dev express and laying out in dreamweaver MX. I find the layout
tools in web express difficult to use and implementation of complex
templates much easier in dwmx.

On my site's primary template page, there is a navigation side bar
comprised of two dropdownlst controls that is in the left margin and
used on every page in the site. This is a locked region on the
template. I use code-behind files so the controls functionality, (eg.
OnSelectedIndexChanged) are referenced in the derived .aspx pages as
OnSelectedIndexChanged=category, OnSelectedIndexChanged=Product. These
work fine.

On the default.aspx page (derived from primary.dwt), there are the same
controls (obviously) and the codebehind file is default.aspx.cs.
Everything to this point works.

THE PROBLEM
============
Once I execute the category dropdownlist (using only one of the
dropdownlists as an example -- the other dll is very similar), and
therefore execute the OnSelectedIndexChanged=category method (the code
for which is in the default.aspx.cs page), the category() method takes
the user to a new page, category.aspx, where the user sees some new
content in the BODY region, and where the same dropdownlists are in
the left margin (because the category.aspx page is basesd on the
primary.dwt template).

Executing the ddl controls on the category.aspx page also works fine
because the code for these methods is in the same default.aspx.cs page,
and the codebehind directive is the same because this page is based on
the primary.dwt file (where the codebehind directive is set).


Sorry for the long preface.
So here are the questions:
======================

1. I need to execute other methods in the main body of the
category,aspx page, independent of the funcitonality of the left-hand
drop down lists.

If this new method, specific to the category.aspx page is called, for
example, newmethod(), should I put this code also into the
default.aspx.cs file, or should I have a separate file, say,
category.aspx.cs in order to keep the code for newmethod() separate?

2. Along those lines, from a "good coding" perspective, it is better
to have all the codebehind in one file (e.g. is that what professional
programmers do?) or is it better to break it up somehow (if that's
possible)? E.g have TWO Codefile="somefile.aspx.cs" directives?

3. Is there a better way to organize this code? It seems that even if
I have a .dll with all the code, and then just reference the
class.method from the .DLL , I still have the same problem of where to
put all the code --- especially when one .aspx page needs to reference
multiple methods?

Thanks in advance for any insight.
-Rangy
 
S

Shaun McDonnell

MVPs, correct me if I am wrong but isn't this possible through the use of
partial classes?

Shaun C McDonnell
Solutions Architect
 
F

Flinky Wisty Pomm

It sounds to me like you want either to put your DropDownLists into a
separate control which can be dropped into your pages, or to place the
code for handling them into a base class, so

// your base class can handle functionality common to all your pages

public class MyBaseClass : System.Web.UI.Page
{
protected DropDownList selCategory;

protected void CategoryIndexChanged(object sender, EventArgs e){}
protected void GoToCategoryPage(int categoryId){}
}


// individual pages derive from your base class
public class MyPageOne : MyBaseClass
{
private void NewMethod(){}
}


//you can override your base methods if needed
public class MyPageTwo : MyBaseClass
{
protected void override GoToCategory(int categoryId)
{
DoSomethingSlightlyDifferent(int categoryId);
}
}


public class MyPageTwo : MyBaseClass
{
protected void override GoToCategory(int categoryId)
{
DoSomethingBeforeYouLeave();
base.GoToCategory(categoryId)
}
}
 
R

Ranginald

Thank you for the detailed explanation and code.

If I were to create the dropdownlists as separate controls, what kind
of control would I use to incorporate the .cs code and the .aspx code?
(E.g. server control?) so I wouldn't have to reformat the .aspx pages.

Also, as I am a novice object-oriented programmer, would all of these
classes you suggested above be stored the same .cs file? or compiled
into one .dll?

Thanks!
 
R

Ranginald

I also just realized another problem....

The dropdownlists in the template page are (as before) in the
default.aspx.cs page of code, in the Page_Load().

Now when I pass a parameter to the category.aspx page, I want the
category.aspx page to appropriately populate the body region with a
datagrid. In the past I just used the Page_Load() method, but now I
can't use the page_load() because this newmethod() that takes the
parameter is specific to the category.aspx page.

Can I use some kind of conditional to test what the page title is in
order to determine whether to kick in the newmethod() on the page?

Thanks!
 
F

Flinky Wisty Pomm

Okay...

If you want to create a separate control, the easiest way as a proud
n00b is a usercontrol

There's a quick overview thereof here
[http://www.15seconds.com/issue/020319.htm]

As for your other problem, let me get this straight, you've got a
method that looks like:

protected void Page_Load(object sender, EventArgs e)
{
PopulateDropDownLists();
}

and in your category page you want

protected void Page_Load(...)
{
PopulateCategoryPageFromCategoryIdPassedFromDropDownLists();
}

Only with a more sensible method name? Your solution lies in the class
hierarchy:

public class MyBasePage : System.Web.UI.Page
{
protected void Page_Load(...)
{
PopulateDropDownLists();
DoOtherPageSetupStuff();
}
}

public class CategoryPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
// load category info
PopulateCategoryInfo();
NewMethod();
// call the Page_Load method of the MyBasePage class
// base is a keyword that lets you talk to the class from which
THIS class is derived
base.Page_Load(sender, e);
}
}


Or have I misunderstood?

As for code organisation, each of your classes should really be in a
separate file. You CAN put as many classes into one file as you like,
but it gets messy and you can't find anything.

I compile all my page classes into a single DLL, core helpers,
controls, and data access get their own DLLs too, but I'm a control
freak and use NAnt so I'm not really qualified to advise you on
compilation with Visual Studio.

If it helps, though, I have a folder structure

/bin
/build
/src
/pages
/data
/core
/controls

etc. Each directory corresponds to a namespace (eg.
MyCompany.ProjectName.Pages) and each namespace corresponds to a DLL
for the purposes of building.
 
J

Jim Cheshire

I compile all my page classes into a single DLL, core helpers,
controls, and data access get their own DLLs too, but I'm a control

Be careful with this approach. It's fine as long as you don't have too
many assemblies. However, keep in mind that each of these assemblies
gets loaded into the process at a random address on a 64k boundary.
This can lead to fragmentation and OOM if you have a lot of them.

Jim Cheshire
Blog: http://blogs.msdn.com/jamesche
 
R

Ranginald

You have not misunderstood at all!!!!! lThat's EXACTLY what I wanted
to do. Thanks so much!!!!!!!

I. So the default.aspx.cs page will have the MyBaseClass, and the
category.aspx.cs page will have the CategoryPage : MyBaseClass.

Is that correct?


II. I really like your method of separating out each class into a
single file -- it makes sense for code cleanliness.

I figure that if I were to use project.aspx.cs as a single codebehind
file for the whole project, the page directive would be something like
codefile="project.aspx.cs" (and won't change) and inherits would be
inherits="whatever class I need in that file" and would change per
..aspx page per whatver class I need.

But if, like you say, you are to use two class files how does each
class file know about each other?

e.g.
if default.aspx.cs has MyBaseClass: System.Web.UI.Page
and category.aspx.cs has CategoryPageClass: MyBaseClass.

how does the category.aspx CategoryPageClass class know where to "find
out/be inherited from" MyBaseClass if they are in different files?

Thanks for your continued help.
 
F

Flinky Wisty Pomm

Better than default/category try

BasePage.cs - base class
Default.aspx.cs - Your default page inherits from base
Category.aspx.cs - Your category page inherits from base

That way you've got a better separation between what is common to
everything, and what goes on your default.aspx

How does each class file know about the other? They get compiled
together. I'm not good with Visual Studio, so you might be better
double checking that with someone else. What you have to remember is
that the pages aren't using your .cs files - the .cs is being compiled
to an assembly.

Control freak that I am, I do this myself so that I know where
everything is - I haven't the foggiest how Visual Studio does it
because I find it unbearably slow for a glorified text editor.
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top