ListView控件的DataItem显示空 [英] ListView DataItem Shows Null

查看:123
本文介绍了ListView控件的DataItem显示空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天,我写的问题与实施ASP.NET一个ListView。现在,所有的写的,我无法保存在ListView更改的项目。另外code的

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

请注意的几件事情:


  • 保存按钮是不正确的ListView的一部分;它调用 GetListViewItems()方法,轮流拨打保存()方法。

  • Listview.DataBind()事件时调用按钮pressed请求记录进行更新

  • 的列表视图显示了使用文本&LT;%#的eval(Key.Name)%&GT; 和<一个href=\"http://stackoverflow.com/questions/595761/concatenating-two-properties-for-display-in-a-dropdownlist\">named 的DropDownList 使用&LT;%#的eval(值)%GT;

  • 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 DropDownList using <%#Eval("Value") %>
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
     }
}

绑定ListView控件

在code数据绑定ListView的是这里显示在我的previous问题


  1. 为什么,当我通过迭代 ListViewDataItem 列表视图每个产品无效

  2. 如何检索 Foo.Id 从字典?

  3. 还有什么我可能会丢失?

  4. 我会用什么,如果我想获得的编号编程基础上分别显示哪些项目?因为它是现在,目前的ListView的依据是什么显示选取美孚秒。那些则S选择的显示方式,并且用户可以修改酒吧的DropDownList ,点击保存,这些变化将会传播。

  1. Why is it that when I iterate through the ListViewDataItem in the Listview that each item is null?
  2. How can I retrieve the Foo.Id from the Dictionary?
  3. What else might I be missing?
  4. What would I use if I wanted to get that Id Programmatically 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 Bar in the DropDownList, hit Save, and those changes are propogated.


事实证明,我的问题是什么<一个href=\"http://stackoverflow.com/questions/609276/listview-dataitem-shows-null/609331#609331\">leppie说了;那就是我需要指定的DataKeyNames ,并使用这些保留从ListView中的信息。

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

这里的code我说:

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

然后在的.ascx 文件,我添加的DataKeyNames =密钥,就像这样:

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

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

这让我使用从我的previous帖子,以确定哪些美孚正在看着。

This allowed me to use the Key from my previous post to determine which Foo was being looked at.

这种方法的任何批评,以及用于制造更好的方法是大大AP preciated。

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

推荐答案

一些快速的答案:


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

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

如果我没有记错,您需要指定的DataKeyNames ,他们可以从 DataKey 属性即可。

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

这篇关于ListView控件的DataItem显示空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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