Newbie: Multiple codebehind files

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
 
C

CJ Taylor

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
 
B

Ben

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
 
J

John Saunders

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.
 
J

John Spiegel

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
 
Joined
Aug 30, 2007
Messages
1
Reaction score
0
Referencing multiple class files

"You can always add these separate classes in .cs files and then reference
them from your codebehind (.aspx.cs) files."

How do you do this? I am able to reference a single custom class file from a codebehind but I have a class file with Authentication functions and the like in one file (ChkAuth.cs) and I have common system functions in another class file (SysFuncs.cs). I am able to reference each individually but do not know how to reference them both.

Below are working examples:

public partial class Default : ChkAuth
{

OR

public partial class Default : SysFuncs
{

but for the life of me I can not figure out how to reference both of these class files.

How is this done?

Thanks in advance for any help that you can give me.

Brent
 

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

Latest Threads

Top