简体   繁体   English

将空列表绑定到ASP.NET中的ListView控件

[英]Binding an empty list to a ListView control in ASP.NET

I have an empty list object ( lstPersons ) that I'm binding to a ListView control ( lvPersons ). 我有一个空列表对象( lstPersons ),该对象绑定到ListView控件( lvPersons )。 Since lstPersons is empty, it seems to never generate the ul in html. 由于lstPersons为空,因此似乎永远不会在html中生成ul。 Is there a way to still force the html ul list to render even though lstPersons is empty? 即使lstPersons为空,有没有办法仍然强制html ul列表呈现? Basically rendering an empty ul, and preferably without having to change the LayoutTemplate? 基本上呈现一个空的ul,并且最好不必更改LayoutTemplate吗?

<asp:ListView ID="lvPersons" runat="server" ClientIDMode="Static">                            
    <LayoutTemplate>                        
        <ul id="ulPersons" runat="server" ClientIDMode="static">
            <li id="itemPlaceholder" runat="server"></li>
        </ul>
    </LayoutTemplate>            
    <ItemTemplate>                                                                                    
        <li>                
            <asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>'></asp:Label>                                                                                                                                                                                                                            
        </li>                         
    </ItemTemplate> 
</asp:ListView>    


List<Person> lstPersons = new List<Person>();
lvPersons.DataSource = lstPersons;
lvPersons.DataBind();

The list exists, but there are no items in it. 该列表存在,但其中没有任何项目。 So if you want something to show up you need to add an empty Person to the list before binding it to the ListView. 因此,如果您想显示某些内容,则需要在将其绑定到ListView之前向列表添加一个空Person

List<Person> lstPersons = new List<Person>();

lstPersons.Add(new Person());

lvPersons.DataSource = lstPersons;
lvPersons.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM