如何在用户控件中公开Panel作为属性?

时间:2010-08-20 23:48:05

标签: asp.net user-controls

我有一个用户控件,上面有一个面板:

CustomControl.ascx

<%@ Control Language="vb" CodeBehind="CustomControl.ascx.vb" %>
<asp:Panel ID="myPanel" runat="server"></asp:Panel>

我希望将该面板公开为我可以在设计时使用的属性。

CustomControl.ascx.vb

<ParseChildren(True), PersistChildren(False)> _
Partial Public Class CustomControl
    Inherits System.Web.UI.UserControl

#Region "Members and Properties"
    <PersistenceMode(PersistenceMode.InnerProperty)> _
    Public Property SomePanel() As Panel
        Get
            Return myPanel
        End Get
        Set(ByVal value As Panel)
            myPanel= value
        End Set
    End Property

#End Region


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         Response.Write(myPanel.HasControls.ToString())

    End Sub

End Class

然后将控件带到这样的页面上:

<ucl:CustomControl runat="server">
    <SomePanel>
        <asp:Label ID="lblText" AssociatedControlID="txtBox" runat="server"></asp:Label>
        <asp:TextBox id="txtBox" runat="server"></asp:TextBox>
    </SomePanel>
</ucl:CustomControl>

当我运行页面时,HasControls为true但没有呈现任何内容。我做错了什么?

1 个答案:

答案 0 :(得分:0)

通过将用户控件设计为模板控件,可以获得类似的效果。如果你选择去模板路线,那么设置并不难。

查看此简要教程以确定控件模板是否适合您:

http://msdn.microsoft.com/en-us/library/36574bf6.aspx