我在处理如何在HTML数据表中显示表的行时遇到麻烦。我想使用asp.net中的c#显示HTML数据表中Mysql表中存储的信息。问题是,代码正在运行,但是HTML表格中仅显示最后一行。请帮忙。代码如下,

C#

protected void ListOfEmployee()
{
    List<EmployeeAccounts> empList = new List<EmployeeAccounts>();
    using (MySqlConnection conn = new MySqlConnection(connectDB()))
    {
        conn.Open();
        MySqlCommand cmd = new MySqlCommand("SELECT * FROM user", conn);
        cmd.CommandType = CommandType.Text;
        cmd.CommandTimeout = 0;
        MySqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            empList.Add(new EmployeeAccounts
            {
                idNumber = dr["user_id"].ToString(),
                uName = dr["username"].ToString(),
                fName = dr["fullname"].ToString(),
                uType = dr["usertype"].ToString(),
                email = dr["email"].ToString(),
                status = dr["status"].ToString()
            });
        }
        dr.Close();
        conn.Close();
    }
}

public class EmployeeAccounts {
    public string idNumber;
    public string uName;
    public string fName;
    public string uType;
    public string email;
    public string status;
}


ASPX

<asp:Table ID="tblUserList" CssClass="table table-hover table-vcenter" runat="server">
<asp:TableHeaderRow TableSection="TableHeader">
<asp:TableHeaderCell >#</asp:TableHeaderCell>
<asp:TableHeaderCell >Full Name</asp:TableHeaderCell>
<asp:TableHeaderCell >Email</asp:TableHeaderCell>
<asp:TableHeaderCell >Status</asp:TableHeaderCell>
<asp:TableHeaderCell >User Type</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell ID="cellID"></asp:TableCell>
<asp:TableCell ID="cellName"></asp:TableCell>
<asp:TableCell ID="cellEmail"></asp:TableCell>
<asp:TableCell CssClass="label label-table label-info" ID="cellStatus"></asp:TableCell>
<asp:TableCell ID="celluType"></asp:TableCell>
</asp:TableRow>
</asp:Table>

最佳答案

List<EmployeeAccounts> lst = GetEmployeeAccounts();

var sb = new System.Text.StringBuilder();

sb.Append(@"
<table class='table table-hover table-vcenter'>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Type</th>
<th>Email</th>
<th>Status</th>
</tr>
");

if(lst.Count==0)
{
    sb.Append("<tr><td colspan='5'>No record</td></tr>");
}
else
{
    foreach(var acc in lst)
    {
        sb.Append("<tr>");
        sb.AppendFormat("<td>{0}</td>", acc.idNumber);
        sb.AppendFormat("<td>{0}</td>", acc.fName);
        sb.AppendFormat("<td>{0}</td>", acc.uType);
        sb.AppendFormat("<td>{0}</td>", acc.email);
        sb.AppendFormat("<td class='label label-table label-info'>{0}</td>", acc.status);
        sb.Append("</tr>");
    }
}

sb.Append("</table>");

string htmlTable = sb.ToString();


在ASP.NET页上添加一个占位符:

<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>


然后在后面的代码处,继续编码:

ph1.Controls.Add(new LiteralControl(htmlTable));


=================================

更新:版本2(2019年10月25日)

List<EmployeeAccounts> lst = GetEmployeeAccounts();

var sb = new System.Text.StringBuilder();

sb.Append(@"
<table class='table table-hover table-vcenter'>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Type</th>
<th>Email</th>
<th>Status</th>
</tr>
");

if (lst.Count == 0)
{
    sb.Append("<tr><td colspan='5'>No record</td></tr>");
}
else
{
    foreach (var acc in lst)
    {
        sb.Append($@"
<tr>
<td>{acc.idNumber}</td>
<td>{acc.fName}</td>
<td>{acc.uType}</td>
<td>{acc.email}</td>
<td class='label label-table label-info'>{acc.status}</td>
</tr>
");
    }
}

sb.Append("</table>");

string htmlTable = sb.ToString();

关于c# - 如何使用asp.net将MySQL表显示为HTML表C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55787591/

10-12 02:38