开发者

How can I render children tags in a custom server control?

I'm working on a custom ServerControl, I've created it like below :

[ParseChildren(true), PersistChildren(true)]
[ToolboxData("<{0}:Menu runat=\"server\"></{0}:Menu>")]
public class Menu : WebControl
{ 
    .....

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [PersistenceMode(PersistenceMode.InnerDefaultProperty)]
    public MenuItem MenuItems { get; set; }
}

[ParseChildren(true), PersistChildren(true)]
public class MenuItem : WebControl
{ 
    ...... 

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public MenuItem SubMenuItems
    {
        get
        {
            if (_SubMenuItems == null) return new MenuItem();
            return _SubMenuItems;
        }
        set
        {
            _SubMenuItems = value;
        }
    }
    private MenuItem _SubMenuItems;

    [TemplateContainer(typeof(MenuItem))]
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate Template { get; set; }
}

<%@ Register Assembly="JQueryMenu" Namespace="JQueryMenu" TagPrefix="MdsMenu" %>
<MdsMenu:Menu ID="Menu1" runat="server">
    <AnimationItems AnimationSpeed="Fast" AnimationType="Opacity_Height" DropShadow="true"
        Delay="1000" />
    <MdsMenu:MenuItem ID="MenuItem1" runat="server" Text="MenuItem 01">
        <MdsMenu:MenuItem runat="server">
            <Template>
                <asp:Button ID="Button1" runat="server" T开发者_如何转开发ext="Button" />
                <asp:CheckBox ID="CheckBox1" runat="server" />
            </Template>
        </MdsMenu:MenuItem>
    </MdsMenu:MenuItem>
    <MdsMenu:MenuItem ID="MenuItem2" runat="server" Text="MenuItem 01">
        <MdsMenu:MenuItem ID="MenuItem3" runat="server">
            <Template>
                <asp:Button ID="Button2" runat="server" Text="Button" />
                <asp:CheckBox ID="CheckBox2" runat="server" />
            </Template>
            <MdsMenu:MenuItem ID="MenuItem5" runat="server" Text="MenuItem 05">
            </MdsMenu:MenuItem>
            <MdsMenu:MenuItem ID="MenuItem6" runat="server" Text="MenuItem 06">
            </MdsMenu:MenuItem>
            <MdsMenu:MenuItem ID="MenuItem4" runat="server">
                <Template>
                    <asp:Image ID="Image1" runat="server" />
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </Template>
            </MdsMenu:MenuItem>
        </MdsMenu:MenuItem>
    </MdsMenu:MenuItem>
</MdsMenu:Menu>

Now, How can I Parse it and render it in RenderContent method ? !!! The following method is always throw the first Exception, it means this.Controls is always empty !!!

How can I do it and how I can access to the nested children in RenderControl method ?

public class Menu : WebControl
{
    ....

    public override void RenderControl(HtmlTextWriter output)
    { 
        if (!this.HasControls())
           throw new Exception("Controls are empty");
        ....
    }
}


Change

[ParseChildren(true)]

to

[ParseChildren(false)]

true indicates that you want the children to be parsed as attributes/properties of your class, not children.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜