使用不同aspx的用户控件,但实现相同

本文关键字:实现 控件 aspx 用户 | 更新日期: 2023-09-27 18:03:20

我想区分用户控件的视觉方面,但使用相同的代码隐藏。也就是说,我想要两个usercontrol with .ascx文件:

后台代码="Uploader.ascx.cs"继承=

"Comptech2.moduli.uploader.Uploader"

通过这种方式,我可以改变视觉方面而不改变后面的代码。

谢谢阿尔贝托。

使用不同aspx的用户控件,但实现相同

为您的usercontrol创建一个基类,并且最终usercontrols(*。

// 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;
    }
}