B
Bennett Haselton
I want to display a hierarchical listing of items from a database
table, where, say, each row in the table has an "ID" field and a
"parent_id" field giving the ID of its parent (NULL if it's at the top
level of the hierarchy) -- like message posts and their replies. Is
there a built-in way to do this, or a generally accepted simplest way?
My first idea was to create a user control like HierarchicalListing
that contains a Repeater, and the repeater's ItemTemplate would itself
contain an instance of HierarchicalListing. Then when putting the
HierarchicalListing on the page, you set its DataSource and have it
query for all rows where the parent_id is null, and databind the
Repeater to the results -- and then inside the Repeater, look at the
ID of the current row, and for the HierarchicalListing inside the
Repeater, have it query for all rows where the parent_id was equal to
that ID. That would give you a hierarchical listing of all the rows,
if ASP.Net allowed it.
Unfortunately if you try adding
<%@ Register TagPrefix="uc1" TagName="HierarchicalPostListing"
Src="HierarchicalPostListing.ascx" %>
to the top of HierarchicalPostListing.ascx itself, you get a run-time
error that circular file references are not allowed.
So, is there a way for a user control to be nested inside itself (or
rather, to be nested conditionally inside itself, so you don't get an
infinite recursion)? More importantly, is there a generally accepted
way to do a hierarchical listing like what I'm trying to do?
-Bennett
table, where, say, each row in the table has an "ID" field and a
"parent_id" field giving the ID of its parent (NULL if it's at the top
level of the hierarchy) -- like message posts and their replies. Is
there a built-in way to do this, or a generally accepted simplest way?
My first idea was to create a user control like HierarchicalListing
that contains a Repeater, and the repeater's ItemTemplate would itself
contain an instance of HierarchicalListing. Then when putting the
HierarchicalListing on the page, you set its DataSource and have it
query for all rows where the parent_id is null, and databind the
Repeater to the results -- and then inside the Repeater, look at the
ID of the current row, and for the HierarchicalListing inside the
Repeater, have it query for all rows where the parent_id was equal to
that ID. That would give you a hierarchical listing of all the rows,
if ASP.Net allowed it.
Unfortunately if you try adding
<%@ Register TagPrefix="uc1" TagName="HierarchicalPostListing"
Src="HierarchicalPostListing.ascx" %>
to the top of HierarchicalPostListing.ascx itself, you get a run-time
error that circular file references are not allowed.
So, is there a way for a user control to be nested inside itself (or
rather, to be nested conditionally inside itself, so you don't get an
infinite recursion)? More importantly, is there a generally accepted
way to do a hierarchical listing like what I'm trying to do?
-Bennett