ASP.NET:构建一个接受列表作为参数的用户控制? [英] ASP.NET: Build user control that take a list as parameter?

查看:84
本文介绍了ASP.NET:构建一个接受列表作为参数的用户控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能建立一个用户控件,需要一个列表作为一个参数,即:

 <富:TabMenu =服务器>
<&标签GT;
<选项卡标签=TAB1PanelId =pnlTab1/>
<选项卡标签=TAB2PanelId =pnlTab2/>
<选项卡标签=TAB3PanelId =pnlTab3/>
< /标签>
< /富:TabMenu>


解决方案

您需要的是这样的。一切都OK,但你必须完成TabCollection类。

编辑:对不起,我没有测试code。无论如何发现了一些问题,所以解决方法。

用户控件

  [ParseChildren(真正的标签),PersistChildren(假)]
公共部分类TabMenu:用户控件
{    私人TabCollection _tabs;    [可浏览(假),PersistenceMode(PersistenceMode.InnerProperty),MergableProperty(假)]
    公共虚拟TabCollection标签
    {
        得到
        {
            如果(this._tabs == NULL)
                this._tabs =新TabCollection(本);
            返回this._tabs;
        }
    }    保护覆盖的ControlCollection CreateControlCollection()
    {
        返回新TabMenuControlCollection(本);
    }}

标签

 公共类标签:HtmlGenericControl
{    公共字符串标签
    {
        {返回(串)的ViewState [标签]?的String.Empty; }
        集合{的ViewState [标签] =值; }
    }}

TabCollection

 公共类TabCollection:IList的,ICollection的,IEnumerable的
{    私人TabMenu _tabMenu;    公共TabCollection(TabMenu tabMenu)
    {
        如果(tabMenu == NULL)
            抛出新的ArgumentNullException(tabMenu);        this._tabMenu = tabMenu;
    }    公共虚拟INT添加(标签选项卡)
    {
        如果(标签== NULL)
            抛出新的ArgumentNullException(标签);        this._tabMenu.Controls.Add(标签);        返回this._tabMenu.Controls.Count - 1;
    }    INT IList.Add(对象的值)
    {
        返回this.Add((TAB)值);
    }    //你必须写其他的方法和属性添加。}

TabMenuControlCollection

 公共类TabMenuControlCollection:的ControlCollection
{    公共TabMenuControlCollection(TabMenu老板):基地(所有者){}    公共覆盖无效添加(控制子)
    {
        如果(孩子== NULL)
            抛出新的ArgumentNullException(子);        如果(!(孩子TabMenu))
            抛出新的ArgumentException(以下简称TabMenu控制只能有型'标签'的孩子。);        base.Add(小孩);
    }    公共覆盖无效AddAt(INT指数,控制子)
    {
        如果(孩子== NULL)
            抛出新的ArgumentNullException(子);        如果(!(孩子TabMenu))
            抛出新的ArgumentException(以下简称TabMenu控制只能有型'标签'的孩子。);        base.AddAt(索引,儿童);
    }}

How can I build an user control that takes a list as a parameter, i.e:

<foo:TabMenu runat="server">
<Tabs>
<Tab Label="Tab1" PanelId="pnlTab1"/>
<Tab Label="Tab2" PanelId="pnlTab2"/>
<Tab Label="Tab3" PanelId="pnlTab3"/>
</Tabs>
</foo:TabMenu>

解决方案

You need something like this. Everything is ok, but you have to complete the TabCollection class.

Edit: Pardon me, I didn't test the code. Anyway found some problems so resolved them.

UserControl

[ParseChildren(true, "Tabs"), PersistChildren(false)]
public partial class TabMenu : UserControl
{

    private TabCollection _tabs;

    [Browsable(false), PersistenceMode(PersistenceMode.InnerProperty), MergableProperty(false)]
    public virtual TabCollection Tabs
    {
        get
        {
            if (this._tabs == null)
                this._tabs = new TabCollection(this);
            return this._tabs;
        }
    }

    protected override ControlCollection CreateControlCollection()
    {
        return new TabMenuControlCollection(this);
    }

}

Tab

public class Tab : HtmlGenericControl
{

    public string Label
    {
        get { return (string)ViewState["Label"] ?? string.Empty; }
        set { ViewState["Label"] = value; }
    }

}

TabCollection

public class TabCollection : IList, ICollection, IEnumerable
{

    private TabMenu _tabMenu;

    public TabCollection(TabMenu tabMenu)
    {
        if (tabMenu == null)
            throw new ArgumentNullException("tabMenu");

        this._tabMenu = tabMenu;
    }

    public virtual int Add(Tab tab)
    {
        if (tab == null)
            throw new ArgumentNullException("tab");

        this._tabMenu.Controls.Add(tab);

        return this._tabMenu.Controls.Count - 1;
    }

    int IList.Add(object value)
    {
        return this.Add((Tab)value);
    }

    // You have to write other methods and properties as Add.

}

TabMenuControlCollection

public class TabMenuControlCollection : ControlCollection
{

    public TabMenuControlCollection(TabMenu owner) : base(owner) { }

    public override void Add(Control child)
    {
        if (child == null)
            throw new ArgumentNullException("child");

        if (!(child is TabMenu))
            throw new ArgumentException("The TabMenu control can only have a child of type 'Tab'.");

        base.Add(child);
    }

    public override void AddAt(int index, Control child)
    {
        if (child == null)
            throw new ArgumentNullException("child");

        if (!(child is TabMenu))
            throw new ArgumentException("The TabMenu control can only have a child of type 'Tab'.");

        base.AddAt(index, child);
    }

}

这篇关于ASP.NET:构建一个接受列表作为参数的用户控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆