views:

1426

answers:

2

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.

Whats more efficient? Is there any other alternatives?

+1  A: 

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

DOK
I have an architecture of a DAL and BLL set up. I am also using the Enterprise Data Library in the DAL to access my MS SQL Database.The two separate queries would be made by one call in the database. I was referring to two different select statements in the Stored Procedure
TimLeung
Ah, good. I guess I misinterpreted your phrase "doing a select statement in the code-behind".
DOK
What I meant was, when the nested repeater is called to databind, I will then loop through the "tags" result set and find the relevant tags to bind.
TimLeung
Sorry for the confusion...
TimLeung
I see you're relatively new here. It's perfectly OK to edit your question to clarify points like this. And to add the information in your comments about using a DAL and BLL. Sometimes people's answers help you clarify your question for future readers. It's a good question.
DOK
There! I wrote my answer before reading the comments!
flesh
+2  A: 

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

flesh