数据绑定列表与子相关的内容最好的方式(例如,SO的问题与标签) [英] Best Way to DataBind a List with sub-related content (For example, SO's questions with tags)

查看:86
本文介绍了数据绑定列表与子相关的内容最好的方式(例如,SO的问题与标签)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ASP.net 2.0,我怎么present的信息,类似上那么,每个问题有一定的子项(如标签)的问题清单用户。

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.

方法1:

然后,我很可能会使用嵌套的中继器和做在$ C $每个嵌套Repeater C-后面的select语句OnItemDataBind...

Then I would probably be using nested repeaters and doing a select statement in the code-behind on each nested repeater "OnItemDataBind"...

方法二:

或者用两个数据集,我会用C#code创建每个问的公司实体,有一个名为标签属性。然后我会遍历我的标签数据和分配财产。

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?

推荐答案

我肯定会避免第二种方法 - 你不想打你一个数据绑定父项目数据库每次。作为DOK说,尽量妥善建筑师您的系统。对我来说,这将意味着填充业务对象的集合,并绑定到它。我做一个自定义菜单控件(注意,这个巢3 datalists,但你可以使用中继器)类似的东西:

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

在aspx页面:

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

然后在code背后:

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();
}

这种方法基本上是获取对对象的引用绑定(parentList.DataItem),然后结合嵌套DataList控件的子项(_menu.GetChildItems(项目))

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

这篇关于数据绑定列表与子相关的内容最好的方式(例如,SO的问题与标签)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆