gpt4 book ai didi

c# - ASP.NET 中的 List 和 ListViews 的 Dictionary

转载 作者:IT王子 更新时间:2023-10-29 04:44:27 26 4
gpt4 key购买 nike

序言

我问这个问题是因为即使我已经阅读了大量的 ListView 资源,我仍然没有“理解”它。

背景

我有一堆 Foo具有与之关联的项目列表(称为 Bar ),我将它们作为包含 Foo 的字典从数据访问/业务逻辑层中提取出来及其相关的 Bars .我想将网页上的这些项目吐出到 ListView 中持有 Foo.Name在左边,和List<Bar>在下拉列表的右侧。 (下面显示了我美丽的 ASCII 艺术):

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

ListView 代码:

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

问题:

  1. 可以这样使用字典吗?
  2. 关于我哪里出错的任何指示? (资源、提示等会有所帮助)
  3. 有更好的方法吗?
  4. 如果这个问题很清楚,请发表评论,以便我找出改进方法。

最佳答案

您的问题出现是因为在 DataBindFooList() 中对 ddlListOfBars 进行数据绑定(bind)没有意义,因为没有只有一个 DropDownList 可以进行数据绑定(bind)。当您调用 lv.DataBind() 时,ListView 会为每个 Foo 创建一个 ItemTemplate 副本,每个 Foo 都包含一个 ddlListOfBars。您需要告诉它将每个 DropDownList 绑定(bind)到正确的 List 。您可以通过使用数据绑定(bind)表达式而不是在后面的代码中设置 ddlListOfBars 的数据源来做到这一点:

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

请注意,您还需要使用“Key.Name”来获取 Foo 的名称属性。您要绑定(bind)到的各个字典项是 KeyValuePair >,它有一个 Key 属性和一个 Value 属性来分别访问 Foo 和 List

编辑
如果您在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com