Gridview DataBind table 未显示 VB

Gridview DataBind table not showing VB

我正在尝试将数据显示到 ASP.Net 中的 Gridview(使用 VB),但 table 不会出现在我的页面上。 我创建了一个小测试页来查看问题所在,但我似乎找不到任何问题。这是代码:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Test.aspx.vb" Inherits="Test" Title="Test Website" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="Server">
        <div>
            Before Table
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" Width="100%"  ViewStateMode="Enabled">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
                    <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                    <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
                </Columns>
            </asp:GridView>
            After Table
        </div>
</asp:Content>

后台代码如下:

Imports System.Data

Partial Class Test
   Inherits System.Web.UI.Page
   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       If Not IsPostBack() Then
           Dim dt As New DataTable()
           dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                                      New DataColumn("Name", GetType(String)),
                                                      New DataColumn("Country", GetType(String))})
           dt.Rows.Add(1, " Dionysos Greta ", "USA")
           dt.Rows.Add(2, "Traci Khan", "India")
           dt.Rows.Add(3, "Suzanne Malle", "France")
           dt.Rows.Add(4, "Robert Smith", "Whales")
           GridView1.DataSource = dt
           GridView1.DataBind()
       End If
   End Sub
End Class

编辑: 这是页面中显示的图像:

Image of what can be seen when page is loaded

编辑 2:将 AutoEventWireup 设置为“false”后,页面不会加载 GridView。调试时没有出现错误。

谢谢。

设计源不喜欢网格内的 <div> 标签。您似乎在网格中有一个网格。那是你想要的吗?我去掉了多余的网格,效果很好。如果您需要网格中的网格,那么您需要告诉设计内部网格属于外部网格中的列和行。首先你需要在外层网格中添加一些列和行。

<asp:GridView ID="GridView1" runat="server">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
                <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
            </Columns>
</asp:GridView>

我将网格代码卸载到一个单独的 Sub,这样 Page_Load 就不会那么混乱。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        FillGrid()
    End If
End Sub

Protected Sub FillGrid()
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)),
                                               New DataColumn("Name", GetType(String)),
                                               New DataColumn("Country", GetType(String))})
    dt.Rows.Add(1, " Dionysos Greta ", "USA")
    dt.Rows.Add(2, "Traci Khan", "India")
    dt.Rows.Add(3, "Suzanne Malle", "France")
    dt.Rows.Add(4, "Robert Smith", "Whales")
    GridView1.DataSource = dt
    GridView1.DataBind()
End Sub