ASP.NET GridView绑定XML数据后网页无显示问题求助
Let's walk through the issues stopping your GridView from showing up and fix them one by one:
1. Missing @Page Directive (Critical!)
Your index.aspx page doesn't have the required @Page directive, which links the markup to your code-behind class. That means the binding code in Page_Load isn't even running—no wonder the GridView is missing!
Fix: Add this line at the very top of index.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="purchases" %>
Note: Adjust CodeFile to match your actual code-behind file name if it's different.
2. Invalid GridView Markup (Extra Closing Tag)
Looking at your GridView columns, there's an extra </asp:TemplateField> right after the "Unit Price" field. This breaks the control's structure and prevents proper rendering.
Fix: Remove the extra closing tag. Here's the corrected GridView markup:
<asp:GridView ID="purchaseGrid" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblRegistrations_ID" runat="server" Text='<%# Bind ("id")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Full Name"> <ItemTemplate> <asp:Label ID="lblRegistrations_FullName" runat="server" Text='<%# Bind ("fullName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label ID="lbl_Email" runat="server" Text='<%# Bind("emailAddress") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Product Name"> <ItemTemplate> <asp:Label ID="lbl_" runat="server" Text='<%# Bind ("productname") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Unit Price"> <ItemTemplate> <asp:Label ID="lbl_UP" runat="server" Text='<%# Bind ("unitprice") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Unit quantity"> <ItemTemplate> <asp:Label ID="lbl_quantity" runat="server" Text='<%# Bind ("quantity") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
3. Data Source Binding Issue
When you read the XML into a DataSet, it creates a DataTable named Purchase (matching your XML's <Purchase> nodes). Binding directly to the DataSet can cause ambiguity—you need to explicitly target the correct table.
Fix: Update your code-behind's Page_Load method to use the specific DataTable:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Data; public class purchases : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("~/purchases.xml")); // Bind to the Purchase table instead of the whole DataSet purchaseGrid.DataSource = ds.Tables["Purchase"]; purchaseGrid.DataBind(); } }
Final Checks
- Make sure your
purchases.xmlfile is in the root of your project (matchesServer.MapPath("~/purchases.xml")). - Verify that your code-behind file is properly linked (the
CodeFileandInheritsvalues in the@Pagedirective match your file and class names).
After applying these fixes, your GridView should render correctly with all the XML data. Once that's working, you can move on to styling and other enhancements.
内容的提问来源于stack exchange,提问作者Sah J

