• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

Adding conditional statement to a DataList

Being brand new to ASP.net I am struggling with the transition from classic ASP so please bear with me.

I have a DataList that returns the following:

ID      ProductName      SubID      SubName
1      Test product A      NULL      NULL
2      Test product B      1      Sub Product i
2      Test product B      2      Sub Product ii
3      Test product C      NULL      NULL

As you can see, some products have sub products while others do not.  I would like to display these records like this:

Test product A
Test product B
     Sub Product i
     Sub Product ii
Test product C

In ASP I would have used a conditional statement to loop through the recordset determining whether or not to write the Product Name or the Sub Name

How would I accomplish this in .Net 2.0?
  • 3
  • 2
1 Solution
You'll want to look into nested DataLists, which are a pain in my opinion.  You'll basically put a 2nd datalist in your 1st datalist's itemtemplate.

Here's an MSDN article I use for reference.  It's about repeater controls but it would take minor changes to work with a DataList.

cgcmqAuthor Commented:
You're right, that does look awkward.  Would it be possible to write a stored procedure to output in XML and then using the XmlDataSource &  TreeView? Can the XmlDataSource retrieve a stored procedure?
No, but you could write the stored procedure to an in-memory XML file.  I've done this with a treeview before and it worked very well.

And as a matter of fact, I bookmarked the page that showed me how :)


It's towards the bottom of the page.
cgcmqAuthor Commented:
I think that should do it.  It is a lot of code for a newbie but I have the Northwind database so, with luck, I can cut & paste the sample and get it working.  Then I can attempt to rewrite it for my database.

cgcmqAuthor Commented:
You may be seeing more of me as I make this transition from ASP.  No, make that 'will be'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now