Listview没有在asp.net中显示

时间:2014-03-06 07:22:18

标签: c# asp.net listview

我试图在asp.net中实现listview。但我无法得到输出。在哪里我犯了我不知道的错误。请帮帮我。

我的模特

enter image description here

ASPX

 <asp:ListView ID="ListView1" runat="server">
             <ItemTemplate>
                <div>
                <asp:Table runat="server" >
                    <asp:TableRow>
                        <asp:TableCell Width="40%">
                            <asp:Label ID="Label17" runat="server" Font-Bold="true" Font-Size="Medium"  Text='<%#Eval("RoomType") %>'></asp:Label>  
                        </asp:TableCell>
                        <asp:TableCell Width="20%">
                             <asp:Button ID="Button1" runat="server" Text="Search" /> 
                        </asp:TableCell>
                    </asp:TableRow>
                    <asp:TableRow>
                        <asp:TableCell ColumnSpan="2">
                            <asp:Label ID="Label18" runat="server" Font-Bold="true" Font-Size="Medium" Text='<%#Eval("Description") %>'></asp:Label>
                        </asp:TableCell>
                    </asp:TableRow>
                </asp:Table>
                </div>
             </ItemTemplate>
        </asp:ListView>

C#

     public class Place
            {
                public string RoomType { get; set; }

                public string Description { get; set; }
            }

            List<Place> items = new List<Place>();
                        items.Add(new Place() { RoomType = "RoomType1", Description= "RoomType Description"});
                        items.Add(new Place() { RoomType = "RoomType2", Description = "RoomType Description" });
                        items.Add(new Place() { RoomType = "RoomType3", Description = "RoomType Description" });

  ListView1.DataSource = items;

在使用Break point进行检查时,我认识到项目列表中包含项目。但我无法进入我的页面。

enter image description here

2 个答案:

答案 0 :(得分:3)

设置listview的数据源并尝试以下代码

protected void Page_Load(object sender, EventArgs e)
        {
            List<Place> items = new List<Place>();
            items.Add(new Place() { RoomType = "RoomType1", Description = "RoomType Description" });
            items.Add(new Place() { RoomType = "RoomType2", Description = "RoomType Description" });
            items.Add(new Place() { RoomType = "RoomType3", Description = "RoomType Description" });
            this.ListView1.DataSource = items;
            this.ListView1.DataBind();
        }

答案 1 :(得分:2)

我没有看到任何错误。您可以通过将它们写入控制台来检查您添加的列表吗?

Console.WriteLine();
        foreach (Place item in items)
        {
            Console.WriteLine(item);
        }

请添加foreach函数以查看。