Using ASP.net 2.0, how do I present the information to the user similar to the Questions list on SO where each question has some child items (like the tags).
I would probably be making two separate queries, one to first find the list of questions, then another query to find all tags which belonged to the list of questions.
Approach 1:
Then I would probably be using nested repeaters and doing a select statement in the code-behind on each nested repeater ‘OnItemDataBind’…
Approach 2:
Or with the two datasets, I would use C# code to create a business entity of each of the ‘Questions’ and have a property called ‘Tags’. I would then loop through my tags dataset and assign the property.
What’s more efficient? Are there any other alternatives?
I would definitely avoid the second approach – you don’t want to hit the database everytime you databind a parent item. As DOK says, try and architect your system properly. For me that would mean populating a collection of business objects and binding to it. I do something similar with a custom menu control (note this nests three datalists, but you could use repeaters):
In the aspx page:
then in the code behind:
this method basically gets a reference to the object being bound (parentList.DataItem) and then binds the nested DataList to the child items (_menu.GetChildItems(item))