是的,你可以做到这一点...通过继承和创建基类如下
1首先创建一个基类的WebPart类继承与超越CreateChildControls方法如
<XmlRoot("MyWebPartBase")> _
<ToolboxItemAttribute(True)> _
Public Class BaseWebPart
Inherits WebPart
Protected Overrides Sub CreateChildControls()
Dim control As Object = Page.LoadControl(ascxPath)
If control IsNot Nothing Then
control.WebPartControl = Me
Controls.Add(CType(control, Control))
End If
End Sub
'Add public properties here
End Class
2 - 在这个基类中实现你的属性,以及从上面提到的基类而不是webpart类的固有的webparts。
3-为实现公共属性的用户控件创建基类,以便在用户控件中访问它们。
Public Class BaseUserControl
Inherits UserControl
Private _WebPartControl As BaseWebPart
Public Property WebPartControl As BaseWebPart
Get
Return _WebPartControl
End Get
Set(ByVal value As BaseWebPart)
_WebPartControl = value
End Set
End Property
Public ReadOnly Property WebPartID() As String
Get
Return WebPartControl.ID
End Get
End Property
End Class