gpt4 book ai didi

c# - ASP.NET:构建以列表作为参数的用户控件?

转载 作者:行者123 更新时间:2023-11-30 14:19:53 27 4
gpt4 key购买 nike

如何构建一个将列表作为参数的用户控件,即:

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

最佳答案

你需要这样的东西。一切正常,但您必须完成 TabCollection 类。

编辑:请原谅,我没有测试代码。总之发现了一些问题所以解决了。

用户控件

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

}

制表符

public class Tab : HtmlGenericControl
{

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

}

选项卡集合

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

}

关于c# - ASP.NET:构建以列表作为参数的用户控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2055344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com