ListView.DataItem正显示出空 [英] ListView.DataItem is showing null

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

问题描述

列表视图绑定code

 < ASP:内容ID =Content3ContentPlaceHolderID =leftColumnPlaceHolder=服务器>
    < ASP:ListView控件ID =lvQuestions=服务器OnItemDataBound ='lvQuestions_ItemDataBound'&GT​​;
        <&LayoutTemplate模板GT;
            < D​​IV ID =itemPlaceholder=服务器>
            < / DIV>
            < ASP:按钮的ID =btnSubmitAnswers​​=服务器文本=提交答案的OnClick =btnSubmitAnswers_Click/>
        < / LayoutTemplate模板>
        <&ItemTemplate中GT;
            < D​​IV>
                <%#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物业

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 are null on postbacks when you don't DataBind the control again, which is unnecessary when ViewState 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屋!

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