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
ListView DataItem Shows Null
提问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 theSave()
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 namedDropDownList
using<%#Eval("Value") %>
- Save 按钮不是 ListView 的一部分;它调用
GetListViewItems()
方法,方法又调用Save()
方法。 Listview.DataBind()
当按下按钮请求更新记录时调用该事件- Listview 显示文本 using
<%#Eval("Key.Name") %>
和命名DropDownList
using<%#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):
问题):
- Why is it that when I iterate through the
ListViewDataItem
in theListview
that each item isnull
? - How can I retrieve the
Foo.Id
from the Dictionary? - What else might I be missing?
- 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 whatFoo
s were selected. ThoseFoo
s selected are then displayed, and the user can change theBar
in theDropDownList
, hit Save, and those changes are propogated.
- 为什么,当我通过迭代
ListViewDataItem
的Listview
每一个产品null
? - 如何
Foo.Id
从字典中检索? - 我还可能缺少什么?
- 如果我想
Id
根据显示的项目以编程方式获取该信息,我将使用什么?就像现在一样,当前的 ListView 是根据Foo
选择的内容显示的。那些Foo
选择s的则显示,用户可以改变Bar
的DropDownList
,点击保存,以及这些变化将会传播。
Update
更新
As it turns out, my problem was what leppiehad said; and that was that I needed to specify DataKeyNames
and 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 .ascx
file, I added DataKeyNames="Key"
, like so:
然后在.ascx
文件中,我添加了DataKeyNames="Key"
,像这样:
<asp:ListView ID="lvFooList" runat="server" DataKeyNames="Key">
This allowed me to use the Key
from 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:
一些快速答案:
Your need to use databinding for that to work, in other words, assign to
DataSource
and callDataBind()
. EDIT: seems you are doing that. But remember it wont persist between postbacks, just theDataKey
(see below).If I recall correctly, you need to specify the
DataKeyNames
, and they can be retrieved from theDataKey
property then.
您需要使用数据绑定才能工作,换句话说,分配给
DataSource
并调用DataBind()
. 编辑:看来你正在这样做。但请记住,它不会在回发之间持续存在,只是DataKey
(见下文)。如果我没记错的话,您需要指定
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
}