Best Way to DataBind a List with sub-related content (For example, SO’s questions with tags)

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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:

<asp:DataList ID="dlMenuOne" runat="server" onitemdatabound="dlMenu_ItemDataBound" >
            <ItemTemplate>
             //your object

                <asp:DataList ID="dlMenuTwo"  runat="server" onitemdatabound="dlMenuTwo_ItemDataBound">
                <ItemTemplate>
                //your object's child items

                    <asp:DataList ID="dlMenuThree" runat="server">
                    <ItemTemplate>
                       //child item's child items    
                    </ItemTemplate>
                    </asp:DataList>

                </ItemTemplate>
                </asp:DataList> 

            </ItemTemplate>
            </asp:DataList>

then in the code behind:

protected void dlMenu_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataListItem parentList = e.Item;
    DataList dlMenuTwo = (DataList)parentList.FindControl("dlMenuTwo");
    MenuItem item = (MenuItem)parentList.DataItem;
    dlMenuTwo.DataSource = _menu.GetChildItems(item);
    dlMenuTwo.DataBind();
}

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))

Method 2

If you do two separate queries, you can still make them in one call to the database, and get back two resultsets.

Instead of DataSets, you could use the more efficient DataReader (with two resultsets). Loop through the resultsets and populate Question objects with associated Tag objects or properties. Put those objects in an ArrayList or a generic List, and you can bind it to your Repeater.

Instead of doing anything in code-behind, you should consider moving that code to at least a data access layer (class) if you don’t need a business logic layer (another class).

All of these suggestions only apply if your app is non-trivial. Using OO methods does add to the code, but if you are doing anything beyond displaying the information, it would be better to start off with a good OO architecture (DAL and BLL, if not MVC).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x