将dataSource属性与List <dictionary <string,string =“”>&gt;一起使用

时间:2016-01-18 17:24:13

标签: c# asp.net

我有这段代码:

List<Dictionary<string, string>> _DictionaryList = new List<Dictionary<string, string>>();

Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("Max", "Berlin");
dictionary.Add("John", "New York");
dictionary.Add("Mike", "London");
dictionary.Add("Tedd", "Miami");

Dictionary<string, string> dictionary2 = new Dictionary<string, string>();
dictionary2.Add("cat", "Milk");
dictionary2.Add("dog", "Meat");
dictionary2.Add("llama", "Water");

我需要显示如下视图:

enter image description here

我使用asp.net DetailView控件来显示上面的数据,我想使用DataSource控件的DetailView属性。

但问题是如果我使用它:

DetailView1.DataSource = _DictionaryList;
DetailView1.DataBind();

我没有得到所需的视图,我只显示字典和词典2中的项目数。

我的问题是如何制作所需的视图(如上图所示)?

2 个答案:

答案 0 :(得分:0)

您不能将Dictionary作为DataSource绑定到任何数据控件。您可能需要转换为List或动态对象。

答案 1 :(得分:0)

我只是好奇模仿发布的屏幕截图的输出。

标记(aspx):

<div style="width: 250px">
    <asp:Repeater runat="server" ID="repeaterInfo" OnItemDataBound="ItemDataBound">
        <ItemTemplate>
            <asp:Repeater runat="server" ID="rptrInner" ClientIDMode="Predictable">
                <HeaderTemplate>
                    <table style="width:250px; text-align: left;">
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td><%#Eval("Value") %></td>
                        <td><%#Eval("Key") %></td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
            <div style="text-align: center">
                <asp:Repeater runat="server" ID="rptrFooter" ClientIDMode="Predictable">
                    <ItemTemplate>
                        <asp:Label runat="server" Text='<%# Eval("ItemIndex") %>' Font-Bold='<%#Eval("ShouldHighlight") %>' />
                    </ItemTemplate>
                </asp:Repeater>
            </div>
        </ItemTemplate>
    </asp:Repeater>
</div>

代码隐藏:

private List<Dictionary<string, string>> _DictionaryList;

protected void Page_Init(object sender, EventArgs e)
{
    var dictionary = new Dictionary<string, string>
    {
        { "Max", "Berlin" }, 
        { "John", "New York" }, 
        { "Mike", "London" }, 
        { "Tedd", "Miami" }
    };

    var dictionary2 = new Dictionary<string, string>
    {
        { "cat", "Milk" },
        { "dog", "Meat" }, 
        { "llama", "Water" }
    };

    _DictionaryList = new List<Dictionary<string, string>>
    {
        dictionary, 
        dictionary2
    };

    repeaterInfo.DataSource = _DictionaryList;
    repeaterInfo.DataBind();
}

 protected void ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var innerRepeater = e.Item.FindControl("rptrInner") as System.Web.UI.WebControls.Repeater;
        var footerRepeater = e.Item.FindControl("rptrFooter") as System.Web.UI.WebControls.Repeater;

        innerRepeater.DataSource = _DictionaryList[e.Item.ItemIndex];
        innerRepeater.DataBind();

        footerRepeater.DataSource = Enumerable.Range(1, _DictionaryList.Count).Select(i => new { ItemIndex = i, ShouldHighlight = e.Item.ItemIndex + 1 == i });
        footerRepeater.DataBind();
    }
}

输出页面:

browser output of the markup + code