2012-02-06 175 views
0

我正在构建Sharepoint 2010的Web部件。我可以创建可通过Sharepoint用户界面进行编辑的自定义属性。那里没问题。Sharepoint 2010自定义属性

问题是:我想使用自定义对象(Properties.cs)来定义那些相同的属性(并保持编辑功能可用),而不是像在Internet上显示的那样转储Webpart.cs中的所有代码。

有没有办法做到这一点?因为我不想在webpart类中抽取所有属性(可编辑或不可以)。

回答

0

是的,你可以做到这一点...通过继承和创建基类如下

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 
相关问题