2014-04-07 47 views
0

我通常不與這些類型的控制工作,所以我可能是完全在錯誤的軌道這裏..ListView控件綁定到ArrayList和顯示

的最終目標是有記錄的數據集ProductTitle, ProductURL和ProductDescription。然後讓這些記錄以3列格式顯示,如果需要,還可以添加其他行。例如:

記錄1,2,3,4,5,6和7應顯示

1 - 2 - 3 

4 - 5 - 6 

7 

我得到的錯誤是

的System.Web。 HttpException:DataBinding: 'System.Web.UI.WebControls.ListItem'不包含名稱爲'ProductTitle'的 屬性。

前端側:

<div> 
    <asp:ListView ID="ListView1" runat="server" GroupItemCount="3" OnLoad="ListView1_Load"> 

     <LayoutTemplate> 
      <table> 
       <tr> 
        <td> 
         <table border="0" cellpadding="5"> 
          <asp:PlaceHolder runat="server" ID="groupPlaceHolder"></asp:PlaceHolder> 
         </table> 
        </td> 
       </tr> 
      </table> 
     </LayoutTemplate> 

     <GroupTemplate> 
      <tr> 
       <asp:PlaceHolder runat="server" ID="itemPlaceHolder"></asp:PlaceHolder> 
      </tr> 
     </GroupTemplate> 

     <ItemTemplate> 
      <td> 
       <div> 
        <div><%#Eval("ProductTitle")%></div> 
        <img alt="Test Image" src="<%#Eval("ProductURL")%>" /> 
        <div><%# Eval("ProductDescription")%></div> 
       </div> 
      </td> 
     </ItemTemplate> 

    </asp:ListView> 
</div> 

我有我的代碼隱藏:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

End Sub 


Protected Sub ListView1_Load(sender As Object, e As EventArgs) 
    Dim myPaaams As ArrayList = New ArrayList 

    myPaaams.Add(New ListItem("ProductTitle", "Adams Test")) 
    myPaaams.Add(New ListItem("ProductURL", "Adams Test")) 
    myPaaams.Add(New ListItem("ProductDescription", "Adams Test")) 

    ListView1.DataSource = myPaaams 
    ListView1.DataBind() 
End Sub 

我也試着下面的代碼爲我的ListView加載事件,但是失敗時會出現相同的錯誤。

Protected Sub ListView1_Load(sender As Object, e As EventArgs) 
    Dim myParams As ArrayList = New ArrayList 

    Dim variable As New Dictionary(Of String, String) From {{"ProductTitle", "Adams Test"}, _ 
                  {"ProductURL", "value2"}, _ 
                  {"ProductDescription", "value2"}} 

    myParams.Add(variable) 

    ListView1.DataSource = myParams 
    ListView1.DataBind() 
End Sub 

回答

1

你需要使用一個類,如

Public Class Products 
    Public Property ProductTitle As String 
    Public Property ProductURL as String 
    Public Property ProductDescription as String 
End Class 


Protected Sub ListView1_Load(sender As Object, e As EventArgs) 
    Dim myParams As BindingList(of Products)= New BindingList(of Products) 

    Dim p as Products = New Products With {.ProductTitle = "Title", 
             .ProductURL = "URL", 
             .ProductDescription="Description"} 

    myParams.Add(p) 
    ListView1.DataSource = myParams 
    ListView1.DataBind() 
End Sub 

之所以你正在嘗試不工作,是因爲你試圖手動創建一個新的列表項,然後綁定它 - 這是行不通的,因爲列表項沒有正確的屬性。

+0

剛試過這個,但我仍然得到相同的錯誤信息。這個錯誤發生在ListView1.DataBind() – adam

+0

我修復了我的代碼中的拼寫錯誤,將你的aspx代碼複製到一個新的項目中(編輯後將img更改爲div)並將我的代碼粘貼到代碼隱藏中 - 工作)。 – jmoreno

+0

它是有區別的,這是一個ASCX而不是一個ASPX?我們在DNN工作,所有代碼都是在單獨的模塊/控件中完成的,而不是完整的頁面。 – adam