我引用此网站尝试编写代码 - 在ASP.Net C中的HTML表格中显示DataTable #Webform:http://www.aspsnippets.com/Articles/Display-DataTable-in-HTML-Table-in-ASPNet-using-C-and-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;
}
}
}
}
}
}
答案 0 :(得分:3)
注释掉这一行,它会起作用:
PlaceHolder placeholder = new PlaceHolder();
只是一个想法。而不是手动循环DataTable
为什么不使用GridView
控件?
代码背后:
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>
答案 1 :(得分:2)
如果我有分数,我会发表评论但是为了解决你的问题,你需要做的就是注释掉以下一行:
PlaceHolder placeholder = new PlaceHolder();
原因是,您的标记上有一个名为PlaceHolder
的{{1}},然后在加载代码中创建一个全新的placeholder
变量placeholder
。虽然它们的名称相同,但代码将它们视为完全不同的对象。请参阅下面的代码,我借用其他人的代码来创建PlaceHolder
,因为我无法访问您的数据库。
datatable