Alberto
Alberto

Reputation: 199

Usercontrol with different aspx, but same implementation

I want to differentiate the visual aspect of a usercontrol, but using the same code-behind. i.e. I want two usercontrol with in .ascx file:

CodeBehind="Uploader.ascx.cs" Inherits="Comptech2.moduli.uploader.Uploader"

In this way I can change the visual aspect without changing the code behind.

Thanks Alberto

Upvotes: 0

Views: 79

Answers (1)

TcKs
TcKs

Reputation: 26642

Create a base class for your usercontrol, and the final usercontrols (*.aspx files) will derive from base class.

// base class with common functionality
public class MyUserControlBase : UserControl {
    // derived class will initialize this property
    public TextBox TextBox1 {get;set;}
    // derived class will initialize this property
    public Button Button1 {get;set;}

    /* some code of usercontrol */
}

/* ... elsewhere ... */
// final class with *.aspx file
public class MyUserControlA : MyUserControlBase {
    protected override OnInit(EventArgs e) {
        // "this.txtUrl" is generated from *.aspx file
        this.TextBox1 = this.txtUrl;
        // "this.btnSubmit" is generated from *.aspx file
        this.Button1 = this.btnSubmit;
    }
}

/* ... elsewhere ... */
// final class with *.aspx file
public class MyUserControlB : MyUserControlBase {
    protected override OnInit(EventArgs e) {
        // "this.txtTitle" is generated from *.aspx file
        this.TextBox1 = this.txtTitle;
        // "this.btnOk" is generated from *.aspx file
        this.Button1 = this.btnOk;
    }
}

Upvotes: 2

Related Questions