用数据表填充HTML表

本文关键字:HTML 填充 数据表 | 更新日期: 2023-09-27 18:18:46

如果这个问题看起来很奇怪或明显,请提前道歉,但我是c#的新手。

我正在制作一个web应用程序与C#在visual studio,我有一个html table。我想用sql server中的表填充它。

这是我的HTML表:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs"  Inherits="MRAApplication.test" %>
<!DOCTYPE html>
<body>
<form runat="server">
<asp:Button ID="populateButton" runat="server" Text="Table" onClick="populateButton_Click" />
</form>
 <table id="table1">
    <thead>
        <tr>
            <th>AlertID</th>
            <th>AccountID</th>
            <th>Alert</th>
            <th>Active</th>
            <th>IsShow</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
</body>

当我点击button时,我想让表充满我在这里创建的数据表:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
namespace MRAApplication
{
public partial class test : System.Web.UI.Page
{
    protected void populateButton_Click(object sender, EventArgs e)
    {
        GetDataTable();
    }
    public static System.Data.DataTable GetDataTable()
    {
        string queryString = "Select * FROM Alerts";
        string conn =     ConfigurationManager.ConnectionStrings["UATConnectionString"].ToString();
        SqlDataAdapter adapter = new SqlDataAdapter(queryString, conn);
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.TableName = "Table";
        using (System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(queryString, conn))
        {
            da.Fill(dt);
        }
        return dt;
    }
}
}

UATConnectionString正在工作(我几乎是肯定的),所以现在我只需要从我连接到HTMLtable的SQLtable中获取数据。

如果有人能帮助我,我会很感激的。

用数据表填充HTML表

您应该使用Repeater Control。你可以看到一个很好的例子:http://msdn.microsoft.com/en-us/library/zzx23804 (v = vs.85) . aspx