鉴于您的限制,我认为您在共享代码隐藏方面处于正确的轨道上。在您的要求中突出的一件事是能够在不破坏代码隐藏的情况下删除控件。您可以通过在基类(您当前的代码隐藏)和 ascx 之间添加中间代码隐藏来实现此目的。
基类(您重构的代码隐藏)
Public MustInherit Class BaseUC
Inherits System.Web.UI.UserControl
' Define all page controls here
Public MustOverride Property lblPageTitle As Label
Private Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' Check if control has been initialized by inheriting class
If lblPageTitle IsNot Nothing Then
lblPageTitle.Text = "Hooray"
End If
End Sub
End Class
新的特定于站点的代码
Partial Class controls_Sample
Inherits BaseUC
Public Overrides Property lblPageTitle As System.Web.UI.WebControls.Label
Get
Return title
End Get
Set(value As System.Web.UI.WebControls.Label)
title = value
End Set
End Property
End Class
站点特定的 ASCX
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Sample.ascx.vb" Inherits="controls_Sample" %>
<asp:Label runat="server" ID="title"></asp:Label>