使用DataTable填充HTML表

时间:2014-08-19 16:03:55

标签: c# html sql datatable connection-string

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

我正在使用Visual Studio中的C#创建一个Web应用程序,我有一个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正在运行(我几乎是正面的),所以现在我只需要从我连接的SQLtable中获取数据到HTMLtable中。

如果有人能帮助我,我会很感激。如果需要更多信息,我将很乐意提供帮助。

1 个答案:

答案 0 :(得分:2)

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