Best way to DataBind List with subconnected content (e.g. SO questions with tags)

Using ASP.net 2.0, as I present information to a user, a similar list of questions on SO, where each question has some children (for example, tags).

I would probably make two separate queries, one for the first search for the list of questions, then another query for the search for all tags that belong to the list of questions.

Approach 1:

Then I would probably use nested repeaters and execute the select statement in the code for each nested repeater "OnItemDataBind" ...

Approach 2:

Or with two datasets, I would use C # code to create a business object for each of the "Questions" and have the "Tags" property. Then I would scroll through my tag dataset and assign a property.

Which is more efficient? Are there any other alternatives?

+1
source share
2 answers

I would definitely avoid the second approach - you don't want to hit the database every time you bind the parent element. As DOK says, try and organize your system properly. For me, this would mean filling out a collection of business objects and linking to it. I am doing something similar with a custom menu control (note that these are three datalists, but you can use repeaters):

On 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 child items

                    <asp:DataList ID="dlMenuThree" runat="server">
                    <ItemTemplate>
                       //child item 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 associated object (parentList.DataItem) and then binds the nested DataList to the child elements (_menu.GetChildItems (item))

+3
source

, .

DataSets DataReader ( ). Tag. ArrayList , .

, - , , , (), - ( ).

, . OO , - , OO (DAL BLL, MVC).

+1

Source: https://habr.com/ru/post/1699651/


All Articles