开发者

Bind Menu To a List in ASP.NET

How开发者_如何学C to bind a list to ASP.NET Menu control?


try something like this .

This is just example how you bind data to the menu control using asp.net.. you can bind list also same way like this....

Start with a IHierarcyData class that will store each string from the StringCollection...

public class MyMenuItem : IHierarchyData
{
    public MyMenuItem(string s)
    {
        Item = s;
    }

    public override string ToString()
    {
        return Item.ToString();
    }

    #region IHierarchyData Members

    public IHierarchicalEnumerable GetChildren()
    {
        return null;
    }

    public IHierarchyData GetParent()
    {
        return null;
    }

    public bool HasChildren
    {
        get { return false; }
    }

    public object Item
    {
        get; set;
    }

    public string Path
    {
        get { return string.Empty; }
    }

    public string Type
    {
        get { return string.Empty; }
    }

    #endregion
}

Build a class that will be the collection...

public class MyMenu : StringCollection, IHierarchicalEnumerable
{
    List<IHierarchyData> _list = new List<IHierarchyData>();

    public void Add(StringCollection strings)
    {
        foreach (string s in strings)
        {
            MyMenuItem i = new MyMenuItem(s);
            _list.Add(i);
        }
    }

    #region IHierarchicalEnumerable Members

    public IHierarchyData GetHierarchyData(object enumeratedItem)
    {
        return enumeratedItem as IHierarchyData;
    }

    #endregion

    #region IEnumerable Members

    public System.Collections.IEnumerator GetEnumerator()
    {
        return _list.GetEnumerator();
    }

    #endregion
}

In the page you can now construct the menu...

 MyMenu pos = new MyMenu();

 StringCollection sc = new StringCollection();
 sc.Add("First");
 sc.Add("Second");
 pos.Add(sc);

 Menu1.DataSource = pos;
 Menu1.DataBind();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜