[英]How the display the MySQL Table to HTML table using asp.net c#
I am having trouble with working on how to display the rows of the table in an HTML data table. 我在处理如何在HTML数据表中显示表的行时遇到麻烦。 I want to display the information stored on the Mysql Table in HTML data table using c# in asp.net.
我想显示存储在使用C#在asp.net中的HTML数据表中的MySQL表中的信息。 The problem is, the code is working but, only the last row is shown in the table of HTML.
问题是,该代码工作,但是,只有最后排在HTML的表所示。 please help.
请帮忙。 the code follows,
代码如下,
C# 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 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();
add a placeholder at the ASP.NET page: 在ASP.NET页上添加一个占位符:
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
then at the code behind, continue the coding: 然后在后面的代码处继续编码:
ph1.Controls.Add(new LiteralControl(htmlTable));
================================== =================================
Update: Version 2 (Oct 25, 2019) 更新:版本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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.