How to add common properties across custom server controls that inherited different base classes

I need a Server Control (named FormLayout here) that renders child controls based on their attribute.

The goal is to be able to write something like the following:

<custom:FormLayout runat="server" ID="myForm">
   <custom:MyDropDownList runat="server" ID="field1" ColumnSpan="12"></custom:MyDropDownList>
   <custom:MyTextBox runat="server" ID="field2"></custom:MyTextBox>
</custom:FormLayout>

This is the current implementation for these server controls: (In reality there will be more types of child server control, not only these two.)

public interface ICommonFieldProperties{
  public int ColumnSpan {get;set;}
}
public class MyDropDownList:System.Web.UI.WebControls.DropDownList,
 ICommonFieldProperties {
  public int ColumnSpan {get;set;}
}

public class MyTextBox:System.Web.UI.WebControls.TextBox,
 ICommonFieldProperties {
  public int ColumnSpan {get;set;}
}

public class FormLayout:Panel{
protected override void RenderContents(HtmlTextWriter writer){
 foreach (Control i in Controls)
    // can't access the newly added common properties such as "ColumnSpan"
    i.RenderControl(writer);
}

I tried to add the common properties by inheriting a common interface, but then I need to cast these child controls to ICommonFieldProperties whenever I want to access properties defined in it and cast them back to Control whenever I want to use the methods inherited from Control. For exmaple:

protected override void RenderContents(HtmlTextWriter writer) {
  var orderedControls = Controls
    .OfType<ICommonFieldProperties>()
    .Where(c => c.ColumnSpan == 0);
  foreach (var field in orderedControls){
    // Group them into different wrappers based on some other common properties  in ICommon Field
    // ...
    // render
    ((Control)field).RenderControl(writer);
  }
}

Another big problem is that since this is on .Net 4.8, interfaces don’t support default methods so I will need to duplicate every implementation for methods/auto properties in ICommonFieldProperties in each child server control even if they are all the same. Is there an easier way to do this?

Separate FormSlot elements from Control elements.

<custom:FormLayout runat="server" ID="myForm">

   <custom:FormSlot ColumnSpan="12">
      <custom:MyDropDownList runat="server" ID="field1" >
      </custom:MyDropDownList>
   </custom:FormSlot>

   <custom:FormSlot>
      <custom:MyTextBox runat="server" ID="field2">
      </custom:MyTextBox>
   </custom:FormSlot>

</custom:FormLayout>

Since the common properties you mentioned seem to be all layout-related.
If so, it’s better to abstract the FormSlot from Control itself, which acts more like a container for setting arranging properties such as the ColumnSpan you mentioned above.

public class FormSlot: PlaceHolder
{
  public int ColumnSpan {get;set;}
}
public class MyDropDownList:System.Web.UI.WebControls.DropDownList
{
}

public class MyTextBox:System.Web.UI.WebControls.TextBox
{
}

public class FormLayout:Panel
{
   protected override void RenderContents(HtmlTextWriter writer)
   {
      var slots = Controls
        .OfType<FormSlot>()
        .Where(c => c.ColumnSpan == 0);
      // This way you can access the common properties as FormSlot
   }
}

New contributor

RAVN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

public partial class MyUserControl: UserControl
{
  public MyUserControl()
  {
     InitializeComponent();
  }
user control
   public string FirstName`enter code here`
    {
        get { return FirstNameTextBox.Text; }
        set { FirstNameTextBox.Text = value; }
    }

    public string LastName
    {
        get { return LastNameTextBox.Text; }
        set { LastNameTextBox.Text = value; }
    }
}

public partial class MainWindow: Window
{
    public MainWindow()
    {
        InitializeComponent();

        MyUserControlInstance.FirstName = "John";
        MyUserControlInstance.LastName = "Doe";

        string firstName = MyUserControlInstance.FirstName;
        string lastName = MyUserControlInstance.LastName;
    }
}

New contributor

Chinwuba Nnaemeka is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật