2013-07-16 67 views
0

我在VB.NET中創建了一個代碼背後的DataTable。我需要從我的.ASPX頁面調用它並將其顯示爲一個ListView。獲取數據到列表視圖

我後面的代碼是:

Dim table As New DataTable 

' columns in the DataTable. 
table.Columns.Add("Monday", System.Type.GetType("System.Int32")) 
table.Columns.Add("Tuesday", System.Type.GetType("System.Int32")) 
table.Columns.Add("Wednesday", System.Type.GetType("System.Int32")) 
table.Columns.Add("Thursday", System.Type.GetType("System.Int32")) 
table.Columns.Add("Friday", System.Type.GetType("System.Int32")) 
' rows with those columns filled in the DataTable. 
table.Rows.Add(1, 2005, 2000, 4000, 34) 
table.Rows.Add(2, 3024, 2343, 2342, 12) 
table.Rows.Add(3, 2320, 9890, 1278, 2) 
table.Rows.Add(4, 3420, 1234, 4321, 89) 
table.Rows.Add(5, 3240, 6600, 1100, 56) 
table.Rows.Add(6, 4320, 1100, 3243, 23) 
table.Rows.Add(7, 4540, 7645, 4324, 56) 

我想只顯示週一和週二在.aspx頁面上。我將如何做到這一點?目前我有這個:

<asp:ListView ID="Listview1" runat="server"> 
    <LayoutTemplate> 
     <table> 
      <tr> 
       <th> 
       </th> 
       <th> 
        monday 
       </th> 
       <th> 
        tuesday 
       </th> 
       <th> 
        wednesday 
       </th> 
      </tr> 
      <tr id="holder" runat="server"> 
      </tr> 
     </table> 
    </LayoutTemplate> 
    <ItemTemplate> 
     <tr> 
      <!--some code goes here am guessing--> 
     </tr> 
    </ItemTemplate> 
</asp:ListView> 

這個在aspx代碼背後?

Property readonly Data() As DataTable 



End Property 

回答

1

在你的ASPX如下:

<asp:ListView ID="table" runat="server" ItemPlaceholderID="itemPlaceholder"> 
     <LayoutTemplate> 
      <table> 
       <tr> 
        <th> 
        </th> 
        <th> 
         monday 
        </th> 
        <th> 
         tuesday 
        </th> 
       </tr> 
       <tr runat="server" id="itemPlaceholder" /> 
      </table> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <tr id="Tr1" runat="server"> 
       <td> 
        <asp:Label ID="lblmon" runat="Server" Text='<%#Eval("monday") %>' /> 
       </td> 
       <td> 
        <asp:Label ID="lbltues" runat="Server" Text='<%#Eval("tuesday") %>' /> 
       </td> 
      </tr> 
     </ItemTemplate> 
    </asp:ListView> 

而在你vb.net如下:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load 
    Dim table As New DataTable 

    ' columns in the DataTable. 
    table.Columns.Add("Monday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Tuesday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Wednesday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Thursday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Friday", System.Type.GetType("System.Int32")) 
    ' rows with those columns filled in the DataTable. 
    table.Rows.Add(1, 2005, 2000, 4000, 34) 
    table.Rows.Add(2, 3024, 2343, 2342, 12) 
    table.Rows.Add(3, 2320, 9890, 1278, 2) 
    table.Rows.Add(4, 3420, 1234, 4321, 89) 
    table.Rows.Add(5, 3240, 6600, 1100, 56) 
    table.Rows.Add(6, 4320, 1100, 3243, 23) 
    table.Rows.Add(7, 4540, 7645, 4324, 56) 

    Me.table.DataSource = table 
    Me.table.DataBind() 
End Sub 

在ASCX,把這個代碼的方法,並調用它在Page_Load中

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

Private Sub BindData() 
    Dim table As New DataTable 

    ' columns in the DataTable. 
    table.Columns.Add("Monday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Tuesday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Wednesday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Thursday", System.Type.GetType("System.Int32")) 
    table.Columns.Add("Friday", System.Type.GetType("System.Int32")) 
    ' rows with those columns filled in the DataTable. 
    table.Rows.Add(1, 2005, 2000, 4000, 34) 
    table.Rows.Add(2, 3024, 2343, 2342, 12) 
    table.Rows.Add(3, 2320, 9890, 1278, 2) 
    table.Rows.Add(4, 3420, 1234, 4321, 89) 
    table.Rows.Add(5, 3240, 6600, 1100, 56) 
    table.Rows.Add(6, 4320, 1100, 3243, 23) 
    table.Rows.Add(7, 4540, 7645, 4324, 56) 

    Me.table.DataSource = table 
    Me.table.DataBind() 
End Sub 
+1

將我的代碼放在您的頁面中。我更改了ID,並在測試網站上測試了我的代碼,並且它可以正常工作。 – Mez

+1

@mali你管理了嗎? – Mez

+1

嘗試創建一個新網站,添加一個新的默認頁面,並分別粘貼我的.ASPX和.ASPX.VB。你應該很容易掌握它的竅門。 – Mez