2014-03-06 40 views
2

我試圖在asp.net中實現listview。但我無法得到輸出。我犯了我不知道的錯誤。請幫助我。Listview未在asp.net中顯示

我的模型

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

+0

,您可以設置列表視圖的數據源? –

+1

您需要ListView1.DataBind(); – yogi

+0

好瑜伽,我現在解決 – Sagotharan

回答

3

設置列表視圖的數據源,並嘗試下面的代碼

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(); 
     } 
+0

謝謝先生,this.ListView1.DataBind();我錯過了這個 – Sagotharan

+2

@Sagotharan酷,標記爲答案,如果它解決你的查詢 –

+0

我試圖標記,但我只是更新我的檔案,所以網站不能立即標記。 – Sagotharan

2

我沒有看到任何錯誤。你可以通過將它們寫入控制檯來檢查你的列表嗎?

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

請添加一個foreach函數來查看。

+0

謝謝先生,我試過這個,而我不能錯誤和輸出。 – Sagotharan

+1

@Sagotharan所以唯一的問題是你沒有數據源的列表視圖 – goGud

+0

s先生。我沒有注意到這個 – Sagotharan