C# ListView 数据项显示为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/609276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:10:57  来源:igfitidea点击:

ListView DataItem Shows Null

c#asp.netlistview

提问by George Stocker

A few days ago, I wrote about issueswith implementing a ListView in ASP.NET. Now, with all of the other code written, I'm having trouble saving changed items in a ListView.

几天前,我写了关于在 ASP.NET 中实现 ListView 的问题。现在,编写了所有其他代码后,我无法在 ListView 中保存更改的项目。

A few things of note:

一些注意事项:

  • The Save button is not part of the ListView proper; it calls the GetListViewItems()method, which in turns call the Save()method.
  • The Listview.DataBind()event is invoked when a button is pressed requesting the records to be updated
  • The Listview shows text using <%#Eval("Key.Name") %>and a named DropDownListusing <%#Eval("Value") %>
  • Save 按钮不是 ListView 的一部分;它调用GetListViewItems()方法,方法又调用Save()方法。
  • Listview.DataBind()当按下按钮请求更新记录时调用该事件
  • Listview 显示文本 using<%#Eval("Key.Name") %>命名DropDownListusing<%#Eval("Value") %>

Getting The Items From the ListView

从 ListView 中获取项目

public void GetListViewItems()
{
 List<Foo> Result = FooManager.CreateFooList();
 DropDownList ddl = null;
 ListViewItem Item = null;
    try
      {
       foreach (ListViewDataItem item in lvFooList.Items)
         {
          Item = item;
          ddl = ((DropDownList) (Item.FindControl("ddlListOfBars")));
          if (//something is there)
           {
            Foo foo = FooManager.CreateFoo();
            foo.Id = item.DataItemIndex; //shows null
            int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
            foo.barId = barId;
            Result.Add(foo);
           }
         }
      }
   catch (Exception ex)
     {
             //Irrelevant for our purposes
     }
}

DataBinding the ListView

数据绑定 ListView

The code to databind the ListView is shown here in my previous question.

数据绑定 ListView 的代码显示在我之前的问题中

Question(s):

问题):

  1. Why is it that when I iterate through the ListViewDataItemin the Listviewthat each item is null?
  2. How can I retrieve the Foo.Idfrom the Dictionary?
  3. What else might I be missing?
  4. What would I use if I wanted to get that IdProgrammatically based on what items were shown? As it is now, the current ListView is shown based on what Foos were selected. Those Foos selected are then displayed, and the user can change the Barin the DropDownList, hit Save, and those changes are propogated.
  1. 为什么,当我通过迭代ListViewDataItemListview每一个产品null
  2. 如何Foo.Id从字典中检索?
  3. 我还可能缺少什么?
  4. 如果我想Id根据显示的项目以编程方式获取该信息,我将使用什么?就像现在一样,当前的 ListView 是根据Foo选择的内容显示的。那些Foo选择s的则显示,用户可以改变BarDropDownList,点击保存,以及这些变化将会传播。


Update

更新

As it turns out, my problem was what leppiehad said; and that was that I needed to specify DataKeyNamesand use those to retain the information from the ListView.

事实证明,我的问题是leppie所说的话;那就是我需要指定DataKeyNames和使用这些来保留 ListView 中的信息。

Here's the code I added:

这是我添加的代码:

try
{
   int DataKeyArrayIndex = 0;
   foreach (ListViewDataItem item in lvFooList.Items)
     {
      Item = item;
      ddl = ((DropDownList) (Item.FindControl("ddlListOfBars")));
      if (//something is there)
       {
        Foo foo = FooManager.CreateFoo();
        Foo tempFoo = FooManager.CreateFoo();
        if (lvFooList != null)
        {
             tempFoo = ((Foo)(lvFooList.DataKeys[DataKeyArrayIndex].Value));
        }

        foo.Id = tempFoo.Id;
        int barId = int.Parse(ddl.SelectedItem.Value); //works just fine
        foo.barId = barId;
        Result.Add(foo);
        DataKeyArrayIndex++;
     }
   }
}

And then in the .ascxfile, I added DataKeyNames="Key", like so:

然后在.ascx文件中,我添加了DataKeyNames="Key",像这样:

<asp:ListView ID="lvFooList" runat="server" DataKeyNames="Key">

This allowed me to use the Keyfrom my previous postto determine which Foo was being looked at.

这让我可以使用Key我上一篇文章中的来确定正在查看哪个 Foo。

Any critiques of this approach, as well as methods for making it better are greatly appreciated.

非常感谢对这种方法的任何批评,以及使其更好的方法。

采纳答案by leppie

Some quick answers:

一些快速答案:

  1. Your need to use databinding for that to work, in other words, assign to DataSourceand call DataBind(). EDIT: seems you are doing that. But remember it wont persist between postbacks, just the DataKey(see below).

  2. If I recall correctly, you need to specify the DataKeyNames, and they can be retrieved from the DataKeyproperty then.

  1. 您需要使用数据绑定才能工作,换句话说,分配给DataSource并调用DataBind(). 编辑:看来你正在这样做。但请记住,它不会在回发之间持续存在,只是DataKey(见下文)。

  2. 如果我没记错的话,您需要指定DataKeyNames,然后可以从DataKey属性中检索它们。

回答by leppie

you can also use the ListViewDataItem.DataItemIndex property instead of keeping your own index, as in:

您还可以使用 ListViewDataItem.DataItemIndex 属性而不是保留自己的索引,如下所示:

foreach (ListViewDataItem item in MyListView.Items)
{
    // in this example key is a string value
    Foo foo = new Foo(MyListView.DataKeys[item.DataItemIndex].Value as string);

    // do stuff with foo
}