服务器控制的子项

时间:2011-04-17 03:13:02

标签: asp.net custom-server-controls servercontrols web-controls

我一直在尝试创建服务器控件。我一直试图让我的MenuItem包含其他的菜单项,就像这样

<cc1:DynamicMenu ID="DynamicMenu1" runat="server">
    <MenuItems>
        <cc1:MenuItem Text="" Url="">
            <cc1:MenuItem Text="" Url="">
                <cc1:MenuItem Text="" Url=""></cc1:MenuItem>
            </cc1:MenuItem>
            <cc1:MenuItem Text="" Url="">
                <cc1:MenuItem Text="" Url=""></cc1:MenuItem>
            </cc1:MenuItem>                
        </cc1:MenuItem>
    </MenuItems>
</cc1:DynamicMenu>

但我的代码只给了我这个

<cc1:DynamicMenu ID="DynamicMenu1" runat="server">
    <MenuItems>
        <cc1:MenuItem Text="" Url="">No other child elements for MenuItem</cc1:MenuItem>
    </MenuItems>
</cc1:DynamicMenu>

我玩过ParseChildren和PersistChildren交换了真或假的值,我已经没想到了 有人可以帮我解决我的问题,下面是我的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Collections;

namespace Gravity.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[ParseChildren(true), PersistChildren(true)]
[ToolboxData("<{0}:DynamicMenu runat=\"server\"></{0}:DynamicMenu>")]    
public class DynamicMenu : WebControl
{
    public DynamicMenu()
    {

    }

    List<MenuItem> _menuItems;
    [PersistenceMode(PersistenceMode.InnerProperty)]          
    public List<MenuItem> MenuItems
    {
        get
        {
            if(_menuItems == null)
                _menuItems = new List<MenuItem>();
            return _menuItems;
        }
    }
}

[ParseChildren(false), PersistChildren(true)]    
public class MenuItem: INamingContainer
{
    private string _text;
    private string _url;

    public MenuItem(string text, string url)
    {

    }
    public MenuItem()
        : this("", "")
    {

    }

    public string Text
    {
        get
        {
            return _text;
        }
        set
        {
            _text = value;
        }
    }

    public string Url
    {
        get
        {
            return _url;
        }
        set
        {
            _url = value;
        }
    }

    private List<MenuItem> _subMenuItems;       
    public List<MenuItem> SubMenuItems
    {
        get
        {
            if (_subMenuItems == null)
                _subMenuItems = new List<MenuItem>();
            return _subMenuItems;
        }
    }
}
}

2 个答案:

答案 0 :(得分:1)

您需要在OnItemDataBound事件中动态地将自定义控件的实例添加到自身。这将重新诅咒直到你的业务逻辑条件得到满足(即:没有孩子,所以不要添加另一个控件)在OnItemDataBound中。

答案 1 :(得分:1)

我认为你应该使用这些属性:

[ParseChildren(true), PersistChildren(false)]  
public class DynamicMenu 

[ParseChildren(typeof(MenuItem), DefaultProperty = "SubMenuItems", ChildrenAsProperties = true)] 
public class MenuItem