Solved

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

Posted on 2011-02-17
4
738 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now