Apr 14 2005

Inheriting A WebForm From A Custom Class

Category: .NetRory Primrose @ 22:01

For the last couple of days at work, I have been developing a new ASP.Net site. Across all the pages in the site, there are some common methods and properties that need to be used. In the case of this new application, I needed to check if the authenticated user has permissions in Active Directory to see the details of the user they are requesting.

The easiest way to achieve this common functionality is to create a base class that inherits from System.Web.UI.Page. Each WebForm code behind page can then inherit from the custom class instead of the default System.Web.UI.Page class. I hit some interesting problems with developing this solution in the VS IDE. 

The first problem is that the design-time view of the pages usually didn't render because it couldn't identify the custom class. This was caused because I initially had the custom class in the ASP.Net project. This is an issue because the design-time render needs a compiled class to render controls and pages as it doesn't interpret uncompiled code. As the class was in the web project , the assembly may not yet be compiled, or if compiled, it usually can't be resolved. 

To fix this problem, I separated the custom class out into a class library which was referenced by the web project. This meant that when the designer attempted to render the design-time display, it had a compiled assembly that it could resolve and use.

The second problem was that the IDE was continuously locking files in the .Net cache. This caused compiling problems with the solution. I can only guess that this second problem was caused by the custom class being in the web project as they haven't happened since I separated the class out into another project.

Tags:

Comments (3) -

1.
Agnel CJ Kurian Agnel CJ Kurian says:

Can I get my derived class page to Inherit controls placed on the base class page?

2.
Rory Primrose Rory Primrose says:

Hi Agnel.

I think what you mean is that you have a class that inherits from Page like I have done in this post and you want to see if you can have controls on the base class Page be accessible to the derived class.

This isn't possible as the control instances would exist in the derived class instance, not the base class Page. Also, at the point of inheritence, there isn't an instance of the page, therefore child controls don't exist. Inheritence is really a definition thing and can't represent a runtime control hierarchy.

3.
Rory Primrose Rory Primrose says:

Now that I think of it, the title of this post is a little confusing. It really should be "Inheriting a webform into a custom class". It could be misunderstood as the custom class being inherited by Page which would be a little on the difficult side.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading