簡體   English   中英

在ASP.Net C#Webform中的HTML表中顯示DataTable

[英]Display DataTable in HTML Table in ASP.Net C# Webform

我指該網站嘗試編寫代碼-在ASP.Net C#Webform的HTML表中顯示數據表: http : //www.aspsnippets.com/Articles/Display-DataTable-in-HTML-Table-in-ASPNet-using- C和VBNet.aspx

但無法顯示表格信息,顯示空白頁。 我該如何解決此代碼?謝謝。

1.CS.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
`body { font-family: Arial;font-size: 10pt; }`

        table {
            border: 1px solid #ccc;
            border-collapse: collapse;
        }

            table th {
                background-color: #F7F7F7;
                color: #333;
                font-weight: bold;
            }

            table th, table td {
                padding: 5px;
                border-color: #ccc;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div style="margin-left: auto; margin-right: auto; text-align: center;">

            <asp:PlaceHolder ID="placeholder" runat="server" />

        </div>
    </form>
</body>
</html>

2.CS.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Configuration;




public partial class CS : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            PlaceHolder placeholder = new PlaceHolder();

            //Populating a DataTable from database.
            DataTable dt = this.GetData();

            //Building an HTML string.
            StringBuilder html = new StringBuilder();

            //Table start.
            html.Append("<table border = '1'>");

            //Building the Header row.
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<th>");
                html.Append(column.ColumnName);
                html.Append("</th>");
            }
            html.Append("</tr>");

            //Building the Data rows.
            foreach (DataRow row in dt.Rows)
            {
                html.Append("<tr>");
                foreach (DataColumn column in dt.Columns)
                {
                    html.Append("<td>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</td>");
                }
                html.Append("</tr>");
            }

            //Table end.
            html.Append("</table>");
            string strText = html.ToString();

            ////Append the HTML string to Placeholder.
            placeholder.Controls.Add(new Literal { Text = html.ToString() });

        }
    }

    private DataTable GetData()
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 [a01],[a02],[a03] FROM [aaa].[dbo].[bbb]"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataTable dt = new DataTable())
                    {
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }
}

注釋掉這一行,它將起作用:

PlaceHolder placeholder = new PlaceHolder();

只是一個想法。為什么不使用GridView控件而不是手動遍歷DataTable

后面的代碼:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = this.GetData();
        gridView.DataSource = dt;
        gridView.DataBind();
    }
}

ASPX頁面:

<asp:GridView ID="gridView" runat="server"></asp:GridView>

如果我有要點,我會發表評論,但要解決您的問題,您要做的就是注釋掉以下行:

PlaceHolder placeholder = new PlaceHolder();

原因是,您的標記上有一個名為placeholderPlaceHolder ,然后在裝入代碼中創建一個類型為PlaceHolder全新placeholder變量。 雖然它們的名稱相同,但是代碼將它們視為2個完全不同的對象。 請參見下面的代碼, 還我借別人的代碼來創建一個datatable ,因為我沒有訪問您的分貝。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        /*
        Commented out because doing it this way creates 
        2 PlaceHolder variables named placeholder, everything else is as needed
        */
        //PlaceHolder placeholder = new PlaceHolder();

        //Populating a DataTable from database.
        DataTable dt = this.GetData();
        //Building an HTML string.
        StringBuilder html = new StringBuilder();
        //Table start.
        html.Append("<table border = '1'>");
        //Building the Header row.
        html.Append("<tr>");
        foreach (DataColumn column in dt.Columns)
        {
            html.Append("<th>");
            html.Append(column.ColumnName);
            html.Append("</th>");
        }
        html.Append("</tr>");
        //Building the Data rows.
        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr>");
            foreach (DataColumn column in dt.Columns)
            {
                html.Append("<td>");
                html.Append(row[column.ColumnName]);
                html.Append("</td>");
            }
            html.Append("</tr>");
        }
        //Table end.
        html.Append("</table>");
        string strText = html.ToString();
        ////Append the HTML string to Placeholder.
        placeholder.Controls.Add(new Literal { Text = html.ToString() });
    }
}
private DataTable GetData()
{
    // Modified your method, since I don't have access to your db, so I created one manually
    // Here we create a DataTable with four columns.
    DataTable table = new DataTable();
    table.Columns.Add("Dosage", typeof(int));
    table.Columns.Add("Drug", typeof(string));
    table.Columns.Add("Patient", typeof(string));
    table.Columns.Add("Date", typeof(DateTime));

    // Here we add five DataRows.
    table.Rows.Add(25, "Indocin", "David", DateTime.Now);
    table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
    table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
    table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
    table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
    return table;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM