[英]Usercontrol with different aspx, but same implementation
我想區分用戶控件的視覺方面,但使用相同的代碼隱藏。 即我想在.ascx文件中使用兩個usercontrol:
CodeBehind =“Uploader.ascx.cs”Inherits =“Comptech2.moduli.uploader.Uploader”
通過這種方式,我可以在不改變背后的代碼的情況下改變視覺方面。
謝謝Alberto
為usercontrol創建一個基類,最終的usercontrols(* .aspx文件)將從基類派生。
// 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.