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

后端 未结 2 510
一个人的身影
一个人的身影 2021-01-27 04:12

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 pro

2条回答
  •  一个人的身影
    2021-01-27 05:02

    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:

    
                
                 //your object
    
                    
                    
                    //your object's child items
    
                        
                        
                           //child item's child items    
                        
                        
    
                    
                     
    
                
                
    

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

提交回复
热议问题