【问题标题】:Gridview DataBind table not showing VBGridview DataBind 表不显示 VB
【发布时间】:2021-07-20 11:37:30
【问题描述】:

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

<%@ 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。调试时不会出错。

谢谢。

【问题讨论】:

  • 代码看起来没问题,你调试了吗?
  • 我确实对其进行了调试,但似乎没有出现任何错误。代码运行正常,只是gridview没有加载。

标签: asp.net vb.net gridview data-binding


【解决方案1】:

设计源不喜欢网格内的&lt;div&gt; 标签。您似乎在网格中有一个网格。那是你的意图吗?我摆脱了额外的网格,它工作正常。如果您需要网格中的网格,那么您需要告诉设计内部网格属于外部网格中的哪一列和哪一行。首先,您需要在外部网格中添加一些列和行。

<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

【讨论】:

  • 好的,谢谢!这似乎有效!
  • @pleasehelpanoob 如果我的回答帮助您解决了您的问题,请点击答案左侧的复选标记(勾号)接受它。这将帮助未来的读者,为您赢得一些积分,也为我赢得一些积分。这是在 Stack Overflow 上感谢人们的最佳方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多