Pitfalls of creating a base class for a webpage including UI contr

M

Me

Hi,

I have inherited an application that contains 7 different but similar forms
(about 15 questions are the same on all forms) that a user can fill out.
Each page is a separate webform containing duplicate UI (for the same
questions) and the code to process these questions is duplicated on all of
the pages.

I'd obviously like to reduce the code duplication and i have come up with
three options.

1) Create a composit control for each question which would contain the UI
and code (validation, saving to object before DB, etc) for itself.

2) Create a base code behind page containing all the duplicate stuff... but
this seems like a pain because I'd need to have the UI controls declared in
the base class. However, that would pose a problem in the Design tab of the
IDE because the IDE would try to insert the controls into the super class
which would become a conflict with the base class. I like this method best,
but the designer problem I mentioned would have to be corrected... any ideas
on that?

3) Create a utility class that would contain all the code to handle the UI
controls, but then I'd have to pass references to everything into the utility
class because the Page, Session, and all the controls would be out of scope.

Well, those are the option. I like 2 the best but that designer problem
would have to be fixed, or a workaround would have to be available.

Thanks
 
M

mzk

I'm using a variation on your solution 2, but I have also been frustrated by
VS adding code to my page's codebehind file declaring a control already
defined in the page's base class.

A hack I use is to change the "<@ Page" directive in the .aspx and remove
the "codebehind" attribute (leave the "inherits" attribute obviously). This
seems to obscure the link between the .aspx & the .aspx.cs which stops VS
trying to be helpful with control declarations.

This appears to have a couple of drawbacks:
1. you can no longer right-click, view code on the .aspx file
2. you take responsibility for declaring controls in the .aspx.cs that you
add to the .aspx (not the base class)

Drawback 1 can be slightly improved if you "Show All Files" in the solution
explorer - the .aspx.cs is visible as a child item to the .aspx file and can
be double-clicked.

Not perfect, but may be helpful. Incidentally, if anyone has a *real*
solution to this problem, please share!
 

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
474,141
Messages
2,570,813
Members
47,357
Latest member
sitele8746

Latest Threads

Top