- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我问这个问题是因为即使我已经阅读了大量的 ListView 资源,我仍然没有“理解”它。
我有一堆 Foo
具有与之关联的项目列表(称为 Bar
),我将它们作为包含 Foo
的字典从数据访问/业务逻辑层中提取出来及其相关的 Bars
.我想将网页上的这些项目吐出到 ListView
中持有 Foo.Name
在左边,和List<Bar>
在下拉列表的右侧。 (下面显示了我美丽的 ASCII 艺术):
------------------------------------------------------------------| 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.
----------------- -----------------| 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.
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);
}
<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>
最佳答案
您的问题出现是因为在 DataBindFooList() 中对 ddlListOfBars 进行数据绑定(bind)没有意义,因为没有只有一个 DropDownList 可以进行数据绑定(bind)。当您调用 lv.DataBind() 时,ListView 会为每个 Foo 创建一个 ItemTemplate 副本,每个 Foo 都包含一个 ddlListOfBars。您需要告诉它将每个 DropDownList 绑定(bind)到正确的 List
<ItemTemplate>
<%#Eval("Key.Name") %>
<asp:DropDownList
ID="ddlListOfBars"
runat="server"
DataSource='<%#Eval("Value")%>'
DataValueField="ItemValue"
DataTextField="ItemName" />
</ItemTemplate>
请注意,您还需要使用“Key.Name”来获取 Foo 的名称属性。您要绑定(bind)到的各个字典项是 KeyValuePair
编辑
如果您在 ItemTemplate 中进行了很多操作,那么将内容移出到用户控件中会很有用。用户控件可以具有强类型属性来访问 DataItem,并且可以强类型访问其子控件。这使您可以灵活地使用 ItemDataBound 事件,而无需所有强制转换和 FindControl() 噪音。我怀疑我会在这种情况下打扰,但它会像
<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();
}
关于c# - ASP.NET 中的 List<T> 和 ListViews 的 Dictionary<T>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/583689/
我正在用 Python (2.6) 编写一个应用程序,需要我使用字典作为数据存储。 我很好奇拥有一个大字典是否更节省内存,或者将其分解为许多(很多)较小的字典,然后拥有一个包含对所有较小字典的引用的“
Convert this [ "Cat" : ["A" : 1, "B": 2], "Mat" : ["C" : 3, "D": 4] ] Into [ "A" : 1,
有什么很酷的快速方法可以让两个字典创建第三个字典,以内连接方式将第一个字典的键映射到第二个字典的值? Dictionary dic1 = new Dictionary {{a1,b1},{a2,b2}
我希望将字典相互嵌套,以便容纳 block 的 xy 坐标。所以我会 IDictionary, IDictionary> 键 Dictionary 包含列、行组合,而值 Dictionary 包含 x
在 C# 中,我需要将数据保存在字典对象中,如下所示: Dictionary> MyDict = new Dictionary>(); 现在我意识到,在某些情况下我需要一些其他(不是字典类的)
第一个Dictionary就像 Dictionary ParentDict = new Dictionary(); ParentDict.Add("A_1", "1")
我似乎无法理解这个问题。我需要使用 LINQ 按内部字典的值对字典进行排序。有什么想法吗? 最佳答案 你的意思是你想要所有的值,按内部值排序? from outerPair in outer from
我想建模一个模式,其中响应是字典: { 'id1': { 'type': 'type1', 'active': true, }, 'id2': { 'type':
我有以下代码要添加或更新(如果已经存在)dict()-dict 中的值: if id not in self.steps: self.steps[ id ] = step else:
我有一个包含字典的 Swift 字典,我想使用存储的属性来访问键值: var json = [NSObject:AnyObject]() var title: String { get
我想创建一个 Dictionary>结构,我想提供一个 IEqualityComparer在包含 APerson 的second 字典中作为关键 如果我只有内部字典,那就是 var f = new D
我有一个集合,其中包含如下文档:文档 1: { "company": "ABC" "application": { "app-1": {"earning_from_src_A": 50,
我正在快速学习。 我发现 dictionary 就像 hash 用于 PHP 或其他一些语言。 那我怎么制作dictionary的dictionary呢?? 我有这样的数据 key:J name:jh
这个问题在这里已经有了答案: Explode a dict - Get all combinations of the values in a dictionary (2 个答案) 关闭 5 个月前
我是编程新手,所以如果我的问题看起来很愚蠢,我很抱歉。我想问一下有没有办法从 Multi.Dictionary 返回key当我有值(value)? 这是我的代码: Dim myDict Set myD
我试图找出标准 Ada 库是否配备了“字典”类型(我的意思是:一种以 格式存储值的数据结构,我可以从中检索 value 使用相应的唯一 key)。 这样的数据结构存在吗?如果是这样,有人可以提供一个
我究竟做错了什么?根据我的测试,objDic.exists 永远不会给出 False! dim objDic set objDic = createobject("scripting.
我想创建一个复合类型,其中包含一个字典作为其命名字段之一。但是明显的语法不起作用。我敢肯定有一些我不明白的基本原理。下面是一个例子: type myType x::Dict() end Jul
julia> hotcell2vocab = Dict([(cell, i-1+vocab_start) for (i,cell) in enumerate(h
我有一个简单的问题:我对 Dictionary.Value 集合进行了很多次迭代,这让我很烦,我必须调用 .ToList() 然后才能调用 .ForEach(),因为它似乎没有可枚举的Dictiona
我是一名优秀的程序员,十分优秀!