ListView.DataItem正显示出空 [英] ListView.DataItem is showing null
问题描述
列表视图绑定code
< ASP:内容ID =Content3ContentPlaceHolderID =leftColumnPlaceHolder=服务器>
< ASP:ListView控件ID =lvQuestions=服务器OnItemDataBound ='lvQuestions_ItemDataBound'>
<&LayoutTemplate模板GT;
< DIV ID =itemPlaceholder=服务器>
< / DIV>
< ASP:按钮的ID =btnSubmitAnswers=服务器文本=提交答案的OnClick =btnSubmitAnswers_Click/>
< / LayoutTemplate模板>
<&ItemTemplate中GT;
< DIV>
<%#Container.DataItemIndex + 1%>:<%#的eval(身体)%>
< / DIV>
< ASP:RadioButtonList的ID =rdlAnswers=服务器数据源='<%#的eval(ExamAnswer)%>' DataTextField =身体DataValueField =AnswerId>
< / ASP:RadioButtonList的>
< / ItemTemplate中>
< / ASP:的ListView>
< / ASP:内容>
在拿下面提交按钮click..like ListView的项目,
我们得到qsnItem.DataItem为 NULL
块引用>的foreach(ListViewDataItem qsnItem在lvQuestions.Items)
{
}请建议是怎么回事错在这里。
解决方案的
的DataItem
ASP.NET中的数据绑定的所有网络的控制是空
上回发的时候你不在的DataBind
再控制,这是不必要的,当的ViewState
启用(默认)。所以,你可以使用这些控件在模板得到的值:
的foreach(ListViewDataItem qsnItem在lvQuestions.Items)
{
单选按钮列表rdlAnswers =(单选按钮列表)qsnItem.FindControl(rdlAnswers);
}如果你需要旧值,您需要从数据库加载它们,或使用<$c$c>ListViewUpdatedEventArgs.OldValues$c$c>物业。
Listview binding code
<asp:Content ID="Content3" ContentPlaceHolderID="leftColumnPlaceHolder" runat="server"> <asp:ListView ID="lvQuestions" runat="server" OnItemDataBound='lvQuestions_ItemDataBound'> <LayoutTemplate> <div id="itemPlaceholder" runat="server"> </div> <asp:Button ID="btnSubmitAnswers" runat="server" Text="Submit Answers" OnClick="btnSubmitAnswers_Click" /> </LayoutTemplate> <ItemTemplate> <div> <%# Container.DataItemIndex + 1 %>:<%# Eval("Body") %> </div> <asp:RadioButtonList ID="rdlAnswers" runat="server" DataSource='<%#Eval("ExamAnswer") %>' DataTextField='Body' DataValueField="AnswerId"> </asp:RadioButtonList> </ItemTemplate> </asp:ListView> </asp:Content>
While fetching the listview items on submit button click..like below, we are getting qsnItem.DataItem as NULL.
foreach (ListViewDataItem qsnItem in lvQuestions.Items) { }
Please suggest what is going wrong here.
解决方案The
DataItem
of all databound web-controls in ASP.NET arenull
on postbacks when you don'tDataBind
the control again, which is unnecessary whenViewState
is enabled(default).So you can use the controls in your templates to get the values:
foreach (ListViewDataItem qsnItem in lvQuestions.Items) { RadioButtonList rdlAnswers = (RadioButtonList)qsnItem.FindControl("rdlAnswers"); }
If you need to old values you need to load them from database or use the
ListViewUpdatedEventArgs.OldValues
Property.这篇关于ListView.DataItem正显示出空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!