Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Poor Man's Guide to Webforms Visual Inheritance

Okay, so it isn't true Visual Inheritance, but it seems pretty cool to be nevertheless.  The circled menu-blocks on the screenshot consists of 3 different .ASCX files with each using the same code-behind .CS file.  Why do this?  Each menu-block control uses a different stylesheet, has different dimensions, and other unique qualities which are presentation-specific.  So instead of taking the time to pass each of these properties in a single .ASCX/.CS, since the logic is exactly the same for each menu-block, why duplicate it?  This approach saved me a lot of time, though I can hear coders conversing like Seinfeld and Castanza with, "Not that there's anything wrong with that..."

Each ASCX file starts the same:

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="_genplace.ascx.cs" Inherits="proj.C_Genplace" %>

The control .ASCXs are distinguished in the .ASPX page file by giving each a different tagname when registering, as in

<%@ Register TagPrefix="PROJ" TagName="C_Genplace" Src="/controls/ii/home/_genplace.ascx" %>
<%@ Register TagPrefix="PROJ" TagName="C_Gencenplace" Src="/controls/ii/home/_gencenterplace.ascx" %>

when placing them in the .ASPX with their specific TagName and id, as in

<PROJ:C_Gencenplace id="uc_gencenclients" runat="server"></PROJ:C_Gencenplace>
<PROJ:C_Genplace id="uc_genrightlocs" runat="server"></PROJ:C_Genplace>

and finally in the code-behind with the single class name and ids from the .ASPX

protected proj.C_Genplace uc_gencenclients, uc_genrightlocs;

Posted: Jul 08 2003, 11:19 PM by daveburke | with 2 comment(s)
Filed under:

Comments

Drew Robbins said:

Great idea. I do the same thing with my ASPX files when appropriate.
# July 9, 2003 12:37 AM

TrackBack said:

# August 14, 2003 10:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)