ListView控件的DataItem显示空 [英] ListView DataItem Shows Null
问题描述
前几天,我写的问题与实施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请求记录进行更新 - 的列表视图显示了使用文本
<%#的eval(Key.Name)%>
和<一个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 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") %>
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问题。
- 为什么,当我通过迭代
ListViewDataItem
在列表视图
每个产品无效
? - 如何检索
Foo.Id
从字典? - 还有什么我可能会丢失?
- 我会用什么,如果我想获得的
编号
编程基础上分别显示哪些项目?因为它是现在,目前的ListView的依据是什么显示选取美孚
秒。那些富
则S选择的显示方式,并且用户可以修改酒吧
在的DropDownList
,点击保存,这些变化将会传播。
- 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.
事实证明,我的问题是什么<一个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.
推荐答案
一些快速的答案:
-
您需要使用数据绑定为工作,换句话说,分配给
数据源
并调用的DataBind()
。编辑:看来你是这样做。但是,请记住它回发之间不会持续,只是DataKey
(见下文)。
Your need to use databinding for that to work, in other words, assign to
DataSource
and callDataBind()
. seems you are doing that. But remember it wont persist between postbacks, just theDataKey
(see below).
如果我没有记错,您需要指定的DataKeyNames
,他们可以从 DataKey $ C $检索C>属性即可。
If I recall correctly, you need to specify the DataKeyNames
, and they can be retrieved from the DataKey
property then.
这篇关于ListView控件的DataItem显示空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!