求教:如何编写设计时多编辑区域的自定义控件?

jackking 2010-03-24 09:08:32
由于项目需要,想把一个按钮区域封装成自定义控件方便分发给项目其他人
按钮栏控件有两个区域需要在设计时允许拖放控件,看了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")来访问

哪位兄弟姐妹写过类似的代码,请指点一下
...全文
94 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
duanyuncanyang 2012-12-11
  • 打赏
  • 举报
回复
我是来灌水的。
jackking 2012-12-10
  • 打赏
  • 举报
回复
不好意思啊,太久没上来了,今天也是上来找找资料看看
yywsmj 2010-08-25
  • 打赏
  • 举报
回复
求求了,急用
yywsmj 2010-08-25
  • 打赏
  • 举报
回复
高手请问如何解决的
jackking 2010-04-05
  • 打赏
  • 举报
回复
已解决

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧