468,268 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

One code behind file and multiple ASPX pages?

Me
Hi,

I inherited a web application that has several very similar forms for
gathering user data. User's must choose the form that best fits their needs
and fill it out. Each form has about 15 questions that are the same and a
few that are different.

The problem is that each form contains duplicate UI code and duplicate code
in the code behind to handle these duplicate questions... I know, but I
inherited the app.

I want to get rid of as much code duplication as I can and my first thought
is to have one code behind file that would inherit from System.Web.UI.Page.
Then each of the "real" code behind files would inherit from the
"baseCodeBehind". BaseCodeBehind would contain all the webcontrol
declarations and code to handle these controls, including the event handlers
for buttons and the like. Then each "real" code behind would have the
form-specific controls declared and handled.

It seems like this should work, but in the short time I have reviewed it,
the IDE inserts the control declarations into the "real" code behind and then
i get an error saying that the control has already been declared in the
BaseCodeBehind.

So, is this a viable solution? Are there any other potential problems that
I haven't seen yet?
Nov 19 '05 #1
1 3377
On Tue, 06 Sep 2005 19:21:49 -0500, Me <Me@discussions.microsoft.com>
wrote:
Hi,

I inherited a web application that has several very similar forms for
gathering user data. User's must choose the form that best fits their
needs
and fill it out. Each form has about 15 questions that are the same and
a
few that are different.

The problem is that each form contains duplicate UI code and duplicate
code
in the code behind to handle these duplicate questions... I know, but I
inherited the app.

I want to get rid of as much code duplication as I can and my first
thought
is to have one code behind file that would inherit from
System.Web.UI.Page.
Then each of the "real" code behind files would inherit from the
"baseCodeBehind". BaseCodeBehind would contain all the webcontrol
declarations and code to handle these controls, including the event
handlers
for buttons and the like. Then each "real" code behind would have the
form-specific controls declared and handled.

It seems like this should work, but in the short time I have reviewed it,
the IDE inserts the control declarations into the "real" code behind and
then
i get an error saying that the control has already been declared in the
BaseCodeBehind.

So, is this a viable solution? Are there any other potential problems
that
I haven't seen yet?


I have gotten this to work previously, but by using 1 code-behind file
(not multiple code-behinds inheriting from a base class), that then each
..aspx uses this 1 code-behind file as its code-behind (and inherits from
that class). It's a little cludgey in VS.NET, as Solution Explorer can
get quirky in that it will move the code-behind file around on you, etc.
You can try it and see what I mean.

So create 1 code-behind, then edit the directives on the other .aspx files
to use this file/class....

--
craig
Microsoft MVP - ASP/ASP.NET
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by draganc | last post: by
171 posts views Thread by tshad | last post: by
17 posts views Thread by tshad | last post: by
29 posts views Thread by John Rivers | last post: by
19 posts views Thread by Alan Silver | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.