Newbie: Multiple codebehind files

  • Thread starter Thread starter John Spiegel
  • Start date Start date
J

John Spiegel

Hi All,

I'm not certain this can be done but it sure seems like it. I'd like to use
multiple C# codebehind files in an aspx page. The idea is that a number of
common functions, like wiring data sources, could be done in a similar
fashion for various controls across multiple pages. What makes sense to me
then would be to have an aspx page that uses a codebehind file. That file
would reference classes in a second file that did the common work.

Assuming I'm on the right track, how would you do this? Is it enough to
simply have both .cs files in the same folder or should each be referenced
in the @Page's Inherits and Src attributes? Basically, I think the idea
should work, but I'm missing the details on referencing and location.

For example, a control:

<asp:DropDownList id="ddlWidgets" runat="server" />

would be referenced in the codebehind's Page_Load method:

protected void Page_Load( Object Source, EventArgs Args )
{
if (!IsPostBack)
{
WireUpControl( ddlWidgets, "Widgets" );
}
}

where WireUpControl is a method in a class located in a different file that
takes a reference to the control and a table name and binds that table's
data to the control.

TIA,

John
 
You don't need multiple code behinds, just multiple class files if you
will... In which case you can add event handlers etc...

you could have nothing but your event handlers in your code behind that make
reference to other classes.. or, if you have a page that your going to use a
lot of functions a lot.... inherit it from system.web.ui.page, creating
your own class with those functions you want.

Then inherit that page into all your otherpages... boom...

This is really effective if you have say a search bar on every page... Just
for example.

-CJ
 
Hi John,

Speaking from a C# background, and in an OO sense certainly, you should not
be able to inherit from more than one superclass (unless you are using C or
something). So it makes sense to assume you cannot inherit from more than
one 'codebehind'.

The usual practice is to place commonly used functions in a different
abstract superclass, further up the heirarchy. However, don't lean too
heavily on heirarchies, as they can become difficult to maintain in an
extensibility sense. Consider instead using the strategy pattern as an
alternative?

hth,
Ben
 
John Spiegel said:
Hi All,

I'm not certain this can be done but it sure seems like it. I'd like to use
multiple C# codebehind files in an aspx page. The idea is that a number of
common functions, like wiring data sources, could be done in a similar
fashion for various controls across multiple pages. What makes sense to me
then would be to have an aspx page that uses a codebehind file. That file
would reference classes in a second file that did the common work.

John,

There are "CodeBehind" files, which are the .aspx.cs files. These are the
"other side" of the .aspx files.

Your web project can contain classes which are not related to a single page.
You can always add these separate classes in .cs files and then reference
them from your codebehind (.aspx.cs) files.
 
Thanks guys. Just the kind of stuff I'm needing.

Desperately looking forward to the day when I can honestly drop the
"Newbie:" tag on my subjects... <g>

- John
 
Back
Top