服务器控制的子项目
我一直在尝试创建一个服务器控件。我一直在尝试让我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在 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.
我认为你应该使用这些属性:
和
I think you should use these attribs:
and