求教:如何编写设计时多编辑区域的自定义控件?
由于项目需要,想把一个按钮区域封装成自定义控件方便分发给项目其他人
按钮栏控件有两个区域需要在设计时允许拖放控件,看了MSDN以及在网上找了些资料
虽然实现了多编辑区域的效果,但测试时发现一个问题,就是添加进入的按钮,成了该工具栏的子控件,得通过FindControl("按钮名称")来查找得到,不象Panel或是Wizard,控件拖放进入后,但依然可以在代码页中进入通过该控件ID访问此控件。
以下是写的测试代码:
Namespace UI.WebControls
<ToolboxData("<{0}:GameBox runat=""server""></{0}:GameBox>")> _
<Designer(GetType(GameBoxDesigner))> _
Public Class GameBox
Inherits CompositeControl
Implements INamingContainer
Private _template As ITemplate
Public _box As GameBoxContainer
Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
<PersistenceMode(PersistenceMode.InnerProperty)> _
<DefaultValue(GetType(ITemplate), "")> _
<TemplateContainer(GetType(GameBoxContainer))> _
Public Property View() As ITemplate
Get
Return _template
End Get
Set(ByVal value As ITemplate)
_template = value
End Set
End Property
Protected Overrides Sub CreateChildControls()
Me.Controls.Clear()
_box = New GameBoxContainer
_box.BackColor = Drawing.Color.Beige
If Me.View IsNot Nothing Then
Me.View.InstantiateIn(_box)
End If
Me.Controls.Add(_box)
End Sub
End Class
#Region "容器对象"
Public Class GameBoxContainer
Inherits WebControl
Implements INamingContainer
Protected Overrides ReadOnly Property TagKey() As System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
End Class
#End Region
#Region "设计器"
Public Class GameBoxDesigner
Inherits CompositeControlDesigner
Private mybox As GameBox
Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
MyBase.Initialize(component)
mybox = CType(component, GameBox)
End Sub
Protected Overrides Sub CreateChildControls()
mybox.BackColor = Drawing.Color.Red
Dim box As GameBoxContainer = mybox.Controls(0)
box.Attributes.Add(DesignerRegion.DesignerRegionAttributeName, "0")
End Sub
Public Overrides Function GetDesignTimeHtml(ByVal regions As System.Web.UI.Design.DesignerRegionCollection) As String
Dim edit As EditableDesignerRegion = New EditableDesignerRegion(Me, "edit")
regions.Add(edit)
Return MyBase.GetDesignTimeHtml(regions)
End Function
Public Overrides Function GetEditableDesignerRegionContent(ByVal region As System.Web.UI.Design.EditableDesignerRegion) As String
Dim host As IDesignerHost = Component.Site.GetService(GetType(IDesignerHost))
If host IsNot Nothing AndAlso mybox IsNot Nothing Then
Return ControlPersister.PersistTemplate(mybox.View, host)
End If
Return String.Empty
End Function
Public Overrides Sub SetEditableDesignerRegionContent(ByVal region As System.Web.UI.Design.EditableDesignerRegion, ByVal content As String)
If content Is Nothing Then
Return
End If
Dim host As IDesignerHost = Component.Site.GetService(GetType(IDesignerHost))
If host IsNot Nothing Then
mybox.View = ControlParser.ParseTemplate(host, content)
End If
End Sub
End Class
#End Region
End Namespace
生成后,在设计时可以在区域中拖放控件,但放入的控件可能通过GameBox1.FindControl("控件名")来获取,原来的想法是拖放的控件,在代码页中也可以通过该控件的名称来直接访问
例如:
<cc1:GameBox ID="GameBox1" runat="server">
<View>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</View>
</cc1:GameBox>
上面的TextBox1在代码页中想直接使用TextBox1来访问该TextBox控件,但现在只能是通过GameBox1.FindControl("TextBox1")来访问
哪位兄弟姐妹写过类似的代码,请指点一下
[解决办法]
已解决
[解决办法]
高手请问如何解决的
[解决办法]
求求了,急用
[解决办法]
不好意思啊,太久没上来了,今天也是上来找找资料看看
[解决办法]
我是来灌水的。