Solved

Display a parent-child tree in ASP.NET (VB)

Posted on 2011-02-17
4
740 Views
Last Modified: 2012-05-11
Hi,

I have a category table that effectively has CategoryID, CategoryName and ParentID.  In the table, the 3 top-level categories have a parent of 0 and all remaining categories are children of the top 3.

There is also a presenter table and a presentercategory table that bridges presenters with categories.  It's fields are PresentercatID(PK), CategoryID and PresenterID.

I am now required to display these categories in an ASP.NET (vb) page such that the categories are displayed alphabetically and that the parent/child relationships are exposed through progressive UL-LI HTML tags like this:

<ul>
    <li>grandparent</li>
    <ul>
        <li>parent></li>
        <ul>
            <li>child</li>
        </ul>
        <li>Other parent</li>
    <ul>
    <li>Other grandparent</li>
</ul>

Open in new window


What would be the best approach to doing this?

Thanks in advance!

Best,

Paul
0
Comment
Question by:pmodiano1
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:chrisrbloom
ID: 34919409
I think I can help, but your table structure is a little confusing.  Can you please clarify?

Are there 3 tables?  Categories, Presenters and PresenterCategories?

I'm confused what the PresenterCategories table would be used for (one to many between the other 2 should work?)

0
 

Author Comment

by:pmodiano1
ID: 34919525
Hi Chris,

There are 3 tables because there is a many-to-one relationship between categories and presenters (any presenter may have several categories).  Take a look at the attached image.

 er diagram
Let me know.

Thanks,

Paul.
0
 
LVL 3

Accepted Solution

by:
chrisrbloom earned 500 total points
ID: 34919721
got it.  

OK, what you need to do is set up a couple of queries.

First, you need a query that will give you a list of all the presenterID's in the Presentercat table (distinct values).  Let's call this query qry_AllPresenters

Second, you need to set up a query that gives you all the categoryID's for a given presenter (something like "SELECT CategoryID from presentercat WHERE PresenterID = @PresenterID")  Let's call this query qry_PresenterCategories

I'm not sure what you are using (web page vb.net, VBA, etc so forgive me for not posting code, but I'll give you the idea here)

Set up a While Loop that iterates through the qry_AllPresenters.  While it's on each record, set up a nested loop  that iterates through qry_PresenterCategories WHERE @PresenterID = the current Presenter record you are on.

If you really want to do the <UL> you can append html code to a string value as you go, adding <UL's> and <LI> tags through the list for every record you get.

If you would rather, you can set up a TreeView Control (http://www.beansoftware.com/ASP.NET-Tutorials/Using-TreeView-Control.aspx) which would make this coding much simpler.



0
 

Author Closing Comment

by:pmodiano1
ID: 35700174
The actual solution was close but different
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question