代码之家  ›  专栏  ›  技术社区  ›  chrisofspades

如何将面板公开为用户控件中的属性?

  •  0
  • chrisofspades  · 技术社区  · 14 年前

    我有一个带有面板的用户控件:

    自定义控件.ascx

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

    我希望把这个面板作为一个我可以在设计时使用的属性来公开。

    自定义控件.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 回复  |  直到 14 年前
        1
  •  0
  •   kbrimington    14 年前

    通过将用户控件设计为模板控件,可以达到类似的效果。如果你选择走模板路线,那么设置就不难了。

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

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

    推荐文章