获取数据到listview

时间:2013-07-16 10:44:43

标签: asp.net vb.net listview datatable

我在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 个答案:

答案 0 :(得分: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