This gets converted to a tree type representation in a List<T>. Thats cool.
I now want to bind this to the UI on an ASP.NET page.
What is the best way of doing this? I know I can use a repeater however I somehow need to nest the repeaters dynamically and make sure that the item in question is displayed nicely/clearly under its parent (if the parent has children and children has children
etc...)
Furthermore, I also need to be able to perform an action on that selected item and have the ability to go through all the items being bound to the control which is displaying all this tree structure data.
Think of it as a menu structure where you have the following:
File
Open
Save
Export
To PDF
To Doc
To DocX
Exit
Edit
Cut
Copy
Paste
I want all of this to be displayed/bound to the UI in some way. The data above will be in a List and each menu above contains children. I'm sure you know what I am talking about.
Thank you. The only problem is.... there would be no way to then display a checkbox or any other controls and use that controls functionality. But also when there is also no way to postback to the server for that row. For example we need a column for each
row called "Update", which posts back to the server which would then read the entire row values that the user has modified and do the update to the backend.
ahmedilyas
Member
414 Points
588 Posts
Best way to represent Hierarchy data?
Jan 30, 2013 02:39 PM|LINK
So I have a flat set of data from the DB.
This gets converted to a tree type representation in a List<T>. Thats cool.
I now want to bind this to the UI on an ASP.NET page.
What is the best way of doing this? I know I can use a repeater however I somehow need to nest the repeaters dynamically and make sure that the item in question is displayed nicely/clearly under its parent (if the parent has children and children has children etc...)
Furthermore, I also need to be able to perform an action on that selected item and have the ability to go through all the items being bound to the control which is displaying all this tree structure data.
Think of it as a menu structure where you have the following:
File
Open
Save
Export
To PDF
To Doc
To DocX
Exit
Edit
Cut
Copy
Paste
I want all of this to be displayed/bound to the UI in some way. The data above will be in a List and each menu above contains children. I'm sure you know what I am talking about.
Thoughts and suggestions are welcome!
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Best way to represent Hierarchy data?
Jan 30, 2013 08:46 PM|LINK
How does this differ from your previous question? http://forums.asp.net/t/1873328.aspx/1
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
ahmedilyas
Member
414 Points
588 Posts
Re: Best way to represent Hierarchy data?
Jan 30, 2013 11:41 PM|LINK
This is a UI question (control question) - not an internal data representation question like before.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Best way to represent Hierarchy data?
Jan 31, 2013 04:39 AM|LINK
You can swap the Response.Write calls for StringBuilder.Append calls:
public partial class RecursiveMenu : System.Web.UI.Page { public List<MenuItem> MenuItems = new List<MenuItem>(); protected void Page_Load(object sender, EventArgs e) { MenuItems = new List<MenuItem> { new MenuItem {Id = 1, ParentId = null, MenuName = "File"}, new MenuItem {Id = 2, ParentId = 1, MenuName = "Open"}, new MenuItem {Id = 3, ParentId = 1, MenuName = "Save"}, new MenuItem {Id = 4, ParentId = 1, MenuName = "Export"}, new MenuItem {Id = 5, ParentId = 4, MenuName = "To PDF"}, new MenuItem {Id = 6, ParentId = 4, MenuName = "Word"}, new MenuItem {Id = 7, ParentId = 6, MenuName = "Doc"}, new MenuItem {Id = 8, ParentId = 6, MenuName = "DocX"}, new MenuItem {Id = 9, ParentId = 1, MenuName = "Exit"}, new MenuItem {Id = 10, ParentId = null, MenuName = "Edit"}, new MenuItem {Id = 11, ParentId = 10, MenuName = "Cut"}, new MenuItem {Id = 12, ParentId = 10, MenuName = "Copy"}, new MenuItem {Id = 13, ParentId = 10, MenuName = "Paste"}, new MenuItem {Id = 14, ParentId = 10, MenuName = "Delete"}, new MenuItem {Id = 15, ParentId = 4, MenuName = "To XML"}, new MenuItem {Id = 16, ParentId =13, MenuName = "Paste Special"}, new MenuItem {Id = 17, ParentId = 13, MenuName = "Paste As HTML"} }; var sb = new StringBuilder(); Literal1.Text = WriteItem(null, ref sb); } string WriteItem(int? parentid, ref StringBuilder sb, int level = -1) { var items = MenuItems.Where(m => m.ParentId == parentid); if (items.Any()) { level++; foreach (var item in items) { if (!item.ParentId.HasValue) { sb.Append("<div style=\"float:left;width:150px;\">"); } for (var i = 0; i < level; i++) { sb.Append(" "); } sb.Append(item.MenuName + "<br />"); WriteItem(item.Id, ref sb, level); if (!item.ParentId.HasValue) { sb.Append("</div>"); } } } return sb.ToString(); } } public class MenuItem { public int Id { get; set; } public int? ParentId { get; set; } public string MenuName { get; set; } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
ahmedilyas
Member
414 Points
588 Posts
Re: Best way to represent Hierarchy data?
Jan 31, 2013 09:00 AM|LINK
Thank you. The only problem is.... there would be no way to then display a checkbox or any other controls and use that controls functionality. But also when there is also no way to postback to the server for that row. For example we need a column for each row called "Update", which posts back to the server which would then read the entire row values that the user has modified and do the update to the backend.