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
4

1 回答 1

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
于 2013-07-16T11:46:07.070 回答