473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multiple Codebehind Pages? Possible/Practical?

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

May 1 '06 #1
11 3122
MVPs, correct me if I am wrong but isn't this possible through the use of
partial classes?

Shaun C McDonnell
Solutions Architect
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

May 1 '06 #2
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)
}
}

May 1 '06 #3
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!

May 1 '06 #4
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!

May 2 '06 #5
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(...)
{
PopulateCategoryPageFromCategoryIdPassedFromDropDo wnLists();
}

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.

May 2 '06 #6
On 2 May 2006 01:46:22 -0700, "Flinky Wisty Pomm"
<Pa********@gmail.com> wrote:
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

May 2 '06 #7
Cheers for the heads-up on that, I'll investigate.

May 2 '06 #8
Do you know if the (potential) problem is fixed by using ILMerge?
http://research.microsoft.com/~mbarnett/ilmerge.aspx

May 2 '06 #9
On 2 May 2006 06:39:38 -0700, "Flinky Wisty Pomm"
<Pa********@gmail.com> wrote:
Do you know if the (potential) problem is fixed by using ILMerge?
http://research.microsoft.com/~mbarnett/ilmerge.aspx

Yes. Note, too, that if you are using ASP.NET 2.0, you can use Web
Deployment Projects (which includes a tool called aspnet_merge.exe) to
do the same thing.

More info on that is available at the link provided on the page you
linked to above.

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

May 2 '06 #10
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.

May 2 '06 #11
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.

May 3 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Connolly | last post by:
I have 2 pages referencing the same codebehind file in my project. Originally the pages referenced separate code behind files. Once I changed the reference to the same file, everything worked...
12
by: A.M | last post by:
Hi, Using VS.NET 2003, If i use SRC page attribute instead of CodeBehind, do i still have intelisence and generally IDE support for that? Thanks, Ali
3
by: nevets2001uk | last post by:
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...
2
by: landers | last post by:
Hi All, I have created a vb file that inherits a System.Web.UI.Page. As I am against duplicating source code, I have set the Codebehind attribute on all my aspx pages in the same directory. ...
5
by: GaryDean | last post by:
I have a 1.1 asp.net project that has been converted by the conversion wizard. By and large the app runs allright except for one page... On one of the .aspx pages, the code behind file is not...
2
by: Jack Li | last post by:
Hi, If I declare multiple namspaces, each in a separate file, how do I specify their path with the "using" directives? Say I have 2 namespaces and 1 main function, all in separate files such as...
0
by: gr8oblivion | last post by:
Hi everyone, I'm using .Net 1.1 to create a c# web application... and I want to use Wilson's Master Pages. The problem is, I'm planning on hosting it on Brinkster which does not allow...
6
by: Bob Johnson | last post by:
I'm finally getting around to migrating a "big" ASP.NET 1.1 app to 2.0. I've been reading up on the differences and I'm NOT finding something I was lead to believe was the case a long time ago. I...
2
by: Max2006 | last post by:
Hi, Is there any way to break a web application into separated web projects, so we can re-use pages\? I am trying to put aspx pages and/or ascx pages in separated web projects,
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.