服务器控制的子项目

发布于 2024-11-02 00:40:45 字数 2855 浏览 3 评论 0原文

我一直在尝试创建一个服务器控件。我一直在尝试让我的 MenuItem 像这样包含其他 MenuItems

<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 交换 true 或 false 的值,我已经没有想法了 有人可以帮我解决我的问题吗?下面是我的代码。

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;
        }
    }
}
}

I've been trying to create a server control. I've been trying to get my MenuItem to contain other MenuItems under neath it like this

<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>

but my code only gives me this

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

I have played around with ParseChildren and PersistChildren swaping there values from true or false and I've run out of ideas
could someone please help me with to resolve my problem below is my code.

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;
        }
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-11-09 00:40:45

您需要在 OnItemDataBound 事件中动态地将自定义控件的实例添加到自身。这将重新循环,直到 OnItemDataBound 中满足您的业务逻辑条件(即:没有子项,因此不要添加另一个控件)。

You need to dynamically add an instance of your custom control to itself in the OnItemDataBound event. This will re-curse until your business logic condition is met(ie:no children, so don't add another control) within the OnItemDataBound.

笑忘罢 2024-11-09 00:40:45

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

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

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

I think you should use these attribs:

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

and

[ParseChildren(typeof(MenuItem), DefaultProperty = "SubMenuItems", ChildrenAsProperties = true)] 
public class MenuItem
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文