将从数据库中获取的数据,以HTML表格的形式显⽰1.HTML页⾯
<body>
<form id="form1" runat="server">
<div id="divUsers" runat="server">
</div>
</form>
</body>
2.添加命名空间
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Text;
3. 在Page_Load事件⾥⾯加载表格
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string conString = ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString;
string query = "SELECT * FROM Customers";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
StringBuilder str = new StringBuilder();
StringBuilder str1 = new StringBuilder();
str.Append("<table>");
str.Append("<tr>");
for (int k = 0; k < dt.Columns.Count; k++)
{
str.Append("<th>");
str.Append(dt.Columns[k].ColumnName);
str.Append("</th>");
}
str.Append("<th>");
str.Append("");
str.Append("</th>");
str.Append("</tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
str.Append("<tr>");
for (int k = 0; k < dt.Columns.Count; k++)
{
str.Append("<td>");
str.Append(dt.Rows[i][k]);
str.Append("</td>");
}
str.Append("<td>");
str.Append("<input type='text' ID='txtId" + (i + 1) + "' runat='server' />");
str.Append("</td>");
str.Append("</tr>");
}
str.Append("</table>");
str.Append("<table>");
str.Append("<tr>");
for (int k = 0; k < dt.Columns.Count; k++)
{
str.Append("<th>");
str.Append("Head");
str.Append("</th>");
}
str.Append("</tr>");
for (int i = 0; i < dt.Rows.Count; i++)
{
str.Append("<tr>");
for (int k = 0; k < dt.Columns.Count; k++) {
html网页格式str.Append("<td>");
str.Append("Test");
str.Append("</td>");
}
str.Append("</tr>");
}
str.Append("</table>");
divUsers.InnerHtml = str.ToString();
}
}
}
}
}
}
View Code
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论