views:

162

answers:

2

Hi,

i am working on a project, with a set of usercontrols beeing loaded dynamically think about sort of a portal page with a bunch of widgets (blocks) and all of them are a usercontrol. they have certain things in common, so i made them all derive from a masterBlock usercontrol

now is there a way to also have some output (in the .ascx) file in common? anything i put in the ascx of the masterBlock is not rendered or overwritten by the derived block.

i was wondering if anyone has any hints to get this to work.

+3  A: 

The *.ascx files can not be derived (maybe with some "magic" can). Derived can be only classes, sou you can create a MyUserControlBase class, which can create some common controls/output and provide it by protected/public properties to derived class (MyWeatherUserControl for example) which can common controls/output modify.

Sample code:

public class MyUserControlBase : UserControl {
    private Panel mainPanel;
    protected Panel MainPanel {
        get { return this.mainPanel; }
    }

    public MyUserControlBase() {
        this.mainPanel = new Panel();
        this.Controls.Add( this.mainPanel );
        this.CreateMainPanelContent();
    }

    protected virtual void CreateMainPanelContent() {
        // create default content
        Label lblInfo = new Label();
        lblInfo.Text = "This is common user control.";
        this.MainPanel.Controls.Add( lblInfo );
    }
}

public class MyWeatherUserControl : MyUserControlBase {
    protected override void CreateMainPanelContent() {
        // the base method is not called,
        // because I want create custom content

        Image imgInfo = new Image();
        imgInfo.ImageUrl = "http://some_weather_providing_server.com/current_weather_in_new_york.gif";
        this.MainPanel.Controls.Add ( imgInfo );
    }
}

public class MyExtendedWeatherUserControl : MyWeatherUserControl {
    protected override void CreateMainPanelContent() {
        // the base method is called,
        // because I want only extend content
        base.CoreateMainPanelContent();

        HyperLink lnkSomewhere = new Hyperlink();
        lnkSomewhere.NavigationUrl = "http://somewhere.com";
        this.MainPanel.Controls.Add ( lnkSomewhere );
    }
}
TcKs
+1  A: 

You could override the Render method in the base class to give you custom rendering, and then the subclasses could work with that.

Vinay Sajip