字典< T>名单< T>在ASP.NET列表视图 [英] Dictionary<T> of List<T> and ListViews in ASP.NET

查看:217
本文介绍了字典< T>名单< T>在ASP.NET列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

preamble

我问这个问题,因为即使我已经经历了很多的ListView资源的阅读,我还没有'得到'吧。

背景

我有一大堆的的有与他们(的被称为律师相关的项目列表),而且我从持有及其相关的酒吧。我想在网页在吐这些物品放入一个的ListView 持有的 Foo.Name 的左侧和名单,其中,酒吧和GT; 在右侧的一个DropDownList。 (图中所示为下面我美丽的ASCII艺术):

的ListView

-------------------------------------------------- ----------------
|项目名称| DropDownList的(名单< T>)|
| --------------------------------- | _____________________ |
| foo1 | | BAR1 | V | |
| | | _______________ | ___ | |
-------------------------------------------------- ----------------
| | DropDownList的(名单< T>)|
| | _____________________ |
| foo2的| | BAR2 | V | |
| | | _______________ | ___ | |
-------------------------------------------------- ----------------

好吧,这里发生了什么事情。这是一个ListView;该项目是从数据库中提取到一个词典<富,列表和LT;酒吧GT;> 。我试图从字典中得到键值项目名称下展现出来,并想获得的`名单,其中,T>酒吧,以显示为右侧ListView中的一个DropDownList。

类图

----------------- -----------------
|富| |酒吧|
----------------- -----------------
|标识| |了itemname |
|名称| | ItemValue |
| BarID | | |
----------------- -----------------

因此​​,要回顾一下,我想放在Dictionary.Key姓名到ListView中的左侧,而Dictionary.Value(这恰好是一个列表)到右边一个DropDownList。

所以,对于每一个键/值对,有好多是一个名字和一个下拉列表,将房子每个键的值。

但我遇到了问题(明显),并很希望有人能告诉我什么,我做错了。

code背后:

 保护字典<富,列表和LT;酒吧GT;> FooDictionary
        {
            得到;
            组;
        }

保护无效DataBindFooList(名单< INT> SelectedFoos)
        {
            System.Web.UI.WebControls.ListView LV = lvFooList;

尝试
            {
                字典<富,列表和LT;酒吧GT;> fooDictionary =
                    新的字典<富,列表和LT;酒吧GT;>();

                的foreach(在SelectedFoos INT富)
                {
                 //建立FOOS列表添加为Dictionary.Keys
                 fooDictionary.Add(fooScalar,酒吧)
                }
                FooDictionary = fooDictionary;
                lv.DataSource = FooDictionary;
                lv.DataBind();
                ddlListOfBars.DataSource = FooDictionary;
                ddlListOfBars.DataValueField =ItemValue;
                ddlListOfBars.DataTextField =了itemname;
                ddlListOfBars.DataBind();
            }
            赶上(例外前)
            {
                SetMessage(divFooMsg,无法恢复FOOS:+
                ex.Message,真正的,真实的);
            }
 

ListView控件code:

 < ASP:ListView控件ID =lvFooList=服务器>
   < LayoutTemplate模板>
      < ASP:占位符=服务器ID =itemPlaceholder>< / ASP:占位符>
   < / LayoutTemplate模板>
      < ItemSeparatorTemplate>
         <小时/>
      < / ItemSeparatorTemplate>
   <的ItemTemplate>
      <%#的eval(名称)%>
      < ASP:DropDownList的ID =ddlListOfBars=服务器>< / ASP:DropDownList的>
    < / ItemTemplate中>
   < / ASP:ListView控件>
 


的问题(S):

  1. 是否有可能使用词典这样?
  2. 在我要去哪里错了任何指针? (资源,暗示等将帮助)
  3. 有没有更好的方式来做到这一点?
  4. 如果这个问题是清楚的泥浆,请评论这样我就可以弄清楚如何改进它。
解决方案

您的问题就出现了,因为它没有意义的数据绑定ddlListOfBars在DataBindFooList(),因为不只是一个DropDownList的数据绑定。当你调用lv.DataBind(),在ListView为每个美孚创建您的ItemTemplate的副本,每个包含ddlListOfBars。你需要告诉它每一个DropDownList的绑定到右侧列表<酒吧GT;因为它去。您可以通过设置ddlListOfBars的数据源的数据绑定EX pressions,而不是在后面的code做到这一点:

 <的ItemTemplate>
  <%#的eval(Key.Name)%>
  < ASP:DropDownList的
        ID =ddlListOfBars
        =服务器
        数据源='<%#的eval(值)%>
        DataValueField =ItemValue
        DataTextField =了itemname/>
< / ItemTemplate中>
 

请注意,您还需要使用Key.Name去你的富名称属性。您所结合的个人字典项目KeyValuePair<富,列表和LT;酒吧GT;>中其中有一个Key属性和价值属性来访问Foo和名单,其中,酒吧和GT;分别。

修改
如果你有很多在你的ItemTemplate回事话,就可以移动的内容出到用户控制是有用的。用户控件可以有一个强类型的属性来访问的DataItem,并强类型访问子控件。这让你没有所有的铸造和的FindControl()噪声的ItemDataBound事件的灵活性。我怀疑我懒得在这种情况下,但它会去像

 < ASP:ListView控件ID =lvFooList=服务器>
< LayoutTemplate模板>
  < ASP:占位符=服务器ID =itemPlaceholder>< / ASP:占位符>
< / LayoutTemplate模板>
  < ItemSeparatorTemplate>
     <小时/>
  < / ItemSeparatorTemplate>
<的ItemTemplate>
  < UC:ListViewContents的DataItem ='<%#的Container.DataItem%> />
< / ItemTemplate中>
 

ListViewContents.ascx ...

 < ASP:标签ID =lbName=服务器/>
< ASP:DropDownList的ID =ddlListOfBars=服务器>< / ASP:DropDownList的>
 

ListViewContents.ascx.cs ...

 公共KeyValuePair<富,列表和LT;酒吧GT;> DataItem的
{
    得到;组;
}

保护覆盖无效OnDataBinding(EventArgs的发送)
{
    base.OnDataBinding(E);

    lbName.Text = DataItem.Key.Name;

    ddlListOfBars.DataTextField =了itemname;
    ddlListOfBars.DataValueField =ItemValue;
    ddlListOfBars.DataSource = DataItem.Value;
    ddlListOfBars.DataBind();
}
 

Preamble

I'm asking this question because even though I've read through a lot of ListView resources, I'm still not 'getting' it.

Background

I have a bunch of Foo's that have a list of items associated with them (known as Bar), and I'm pulling them from the Data Access/Business Logic layer as Dictionary that holds a Foo and its associated Bars. I'd like to spit these items out in on the Webpage into a ListView that holds the Foo.Name on the left, and the List<Bar> on the right in a dropdownlist. (Shown with my beautiful ASCII art below):

ListView

------------------------------------------------------------------
|           Name Of Item          |  DropDownList (of List<T>)   |
|---------------------------------|  _____________________       |
|                foo1             |  |     bar1      | v |       |
|                                 |  |_______________|___|       |  
------------------------------------------------------------------
|                                 |  DropDownList (of List<T>)   |
|                                 |  _____________________       |
|                foo2             |  |     bar2      | v |       |
|                                 |  |_______________|___|       |
------------------------------------------------------------------

Alright, here's what's going on. This is a ListView; The items are pulled from a database into a Dictionary<Foo, List<Bar>>. I'm trying to get the Key Value from the dictionary to show up under 'Name of Item', and am trying to get the `List<T> Bar' to show up as a DropDownList on the right side of the ListView.

Class Diagrams

-----------------          -----------------
|   Foo         |          |  Bar          |
-----------------          -----------------
|  Id           |          |  ItemName     |
|  Name         |          |  ItemValue    |
|  BarID        |          |               |
-----------------          -----------------

So to recap, I want to place the Dictionary.Key "Name" into the left side of the ListView, and the Dictionary.Value (which happens to be a list) into a DropdownList on the right side.

So that, for every Key/Value pair, there'd be a Name and a dropdown list that would house each Key's Value.

But I'm running into problems (obviously), and am hoping someone can tell me what I'm doing wrong.

Code Behind:

  protected Dictionary<Foo, List<Bar>> FooDictionary
        {
            get; 
            set; 
        }

protected void DataBindFooList(List<int> SelectedFoos)
        {
            System.Web.UI.WebControls.ListView lv = lvFooList;

try
            {
                Dictionary<Foo, List<Bar>> fooDictionary =
                    new Dictionary<Foo, List<Bar>>();

                foreach (int Foo in SelectedFoos)
                {
                 // Build List of Foos to add as Dictionary.Keys
                 fooDictionary.Add(fooScalar, Bar)                     
                }
                FooDictionary = fooDictionary;
                lv.DataSource = FooDictionary;
                lv.DataBind();
                ddlListOfBars.DataSource = FooDictionary;
                ddlListOfBars.DataValueField = "ItemValue";
                ddlListOfBars.DataTextField = "ItemName";
                ddlListOfBars.DataBind();
            }
            catch (Exception ex)
            {
                SetMessage(divFooMsg, "Unable to retrieve Foos: " + 
                ex.Message, true, true);
            }

The ListView Code:

<asp:ListView ID="lvFooList" runat="server">
   <LayoutTemplate>
      <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
   </LayoutTemplate>
      <ItemSeparatorTemplate>
         <hr />
      </ItemSeparatorTemplate>
   <ItemTemplate>
      <%#Eval("Name") %>
      <asp:DropDownList ID="ddlListOfBars" runat="server"></asp:DropDownList>
    </ItemTemplate>
   </asp:ListView>


The Question(s):

  1. Is it possible to use a Dictionary in this way?
  2. Any pointers on where I'm going wrong? (Resources, hints, etc. would help)
  3. Is there a better way to do this?
  4. If this question is clear as mud, please comment so I can figure out how to improve it.

解决方案

Your problem arises because it doesn't make sense to databind ddlListOfBars in DataBindFooList(), because there isn't just one DropDownList to databind. When you call lv.DataBind(), the ListView creates a copy of your ItemTemplate for each Foo, each containing a ddlListOfBars. You need to tell it to bind each DropDownList to the right List<Bar> as it goes. You can do this by setting the datasource of ddlListOfBars with data binding expressions rather than in the code behind:

<ItemTemplate>
  <%#Eval("Key.Name") %>
  <asp:DropDownList
        ID="ddlListOfBars"
        runat="server"
        DataSource='<%#Eval("Value")%>'
        DataValueField="ItemValue"
        DataTextField="ItemName" />
</ItemTemplate>

Note that you also need to use "Key.Name" to get to the name property of your Foo. The individual dictionary items that you're binding to are KeyValuePair<Foo,List<Bar>>, which has a Key property and a Value property to access the Foo and List<Bar> respectively.

Edit
If you've got a lot going on in your ItemTemplate then it can be useful to move the contents out into a user control. The user control can have a strongly-typed property to access the DataItem, and has strongly-typed access to its child controls. This gets you the flexibility of the ItemDataBound event without all the the casting and FindControl() noise. I doubt I'd bother in this case, but it would go something like

<asp:ListView ID="lvFooList" runat="server">
<LayoutTemplate>
  <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</LayoutTemplate>
  <ItemSeparatorTemplate>
     <hr />
  </ItemSeparatorTemplate>
<ItemTemplate>
  <uc:ListViewContents DataItem='<%# Container.DataItem %>' />
</ItemTemplate>

ListViewContents.ascx...

<asp:Label ID="lbName" runat="server"/>
<asp:DropDownList ID="ddlListOfBars" runat="server"></asp:DropDownList>

ListViewContents.ascx.cs...

public KeyValuePair<Foo,List<Bar>> DataItem
{
    get; set;
}

protected override void OnDataBinding(EventArgs e)
{
    base.OnDataBinding(e);

    lbName.Text = DataItem.Key.Name;

    ddlListOfBars.DataTextField = "ItemName";
    ddlListOfBars.DataValueField = "ItemValue";
    ddlListOfBars.DataSource = DataItem.Value;
    ddlListOfBars.DataBind();   
}

这篇关于字典&LT; T&GT;名单&LT; T&GT;在ASP.NET列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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