0

我有这个 ASPX 代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;


public partial class Default4 : System.Web.UI.Page
{int i;
    public string strAdmin;
    public string strPass;
    public string strLname;
    public string strEmail;
    public string strFname;
    public string str;

    protected void Page_Load(object sender, EventArgs e)
    {
        string dbPath = Server.MapPath(@"App_Data") + "/my_site.mdb";
        string connectionString = @"Data Source='" + dbPath + "';Provider='Microsoft.Jet.OLEDB.4.0';";
        OleDbConnection con = new OleDbConnection(connectionString);
        con.Open();
        string QueryString = "SELECT * FROM tbl_users";
        OleDbCommand cmd = new OleDbCommand(QueryString, con);
        OleDbDataAdapter da = new OleDbDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "tbl");
        con.Close();
        strt = 
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {

            strFname = ds.Tables[0].Rows[i]["first_name"].ToString();
            strLname = ds.Tables[0].Rows[i]["last_name"].ToString();
            strEmail = ds.Tables[0].Rows[i]["user_email"].ToString();
            strPass = ds.Tables[0].Rows[i]["user_password"].ToString();
            strAdmin = ds.Tables[0].Rows[i]["is_admin"].ToString();
            str += String.Format("{0}&nbsp{1}&nbsp{2}&nbsp{3}&nbsp{4}", strFname, strLname, strEmail, strPass, strAdmin);
        }
    }
}

如何在表格中显示此信息以便它更有用?
(我应该在这里添加哪些 html 标签?在哪里?)我希望得到帮助。

4

1 回答 1

1

正如@David 提到的,您应该为此类功能将数据绑定到 DataGrid;但是,如果您确实希望将该数据集呈现为 html 表(出于各种原因,例如样式、密集的 javascript 等),我使用此函数返回数据集的 html 表示形式,然后用返回的 html 填充 div。

    public static string BuildHTMLTable(DataSet dataSet)
    {
        var table = dataSet.Tables[0];
        var tableString = "<table>";

        tableString += "<thead>";

        for (var i = 0; i < table.Columns.Count; i++)
        {
            tableString += "<th>" + table.Columns[i].ColumnName + "</th>";
        }
        tableString += "</thead>";
        tableString += "<tbody>";

        for(var x = 0; x < table.Rows.Count; x++)
        {
            tableString += "<tr>";

            for (var y = 0; y < table.Columns.Count; y++)
            {
                tableString += "<td>";
                tableString += table.Rows[x][y];
                tableString += "</td>";
            }


            tableString += "</tr>";
        }

        tableString += "</tbody>";
        tableString += "</table>";

        return tableString;
    }
于 2012-11-19T19:55:07.597 回答