Я пытаюсь заполнить интерфейсную таблицу html с помощью серверной части c#, но ни один из HtmlTableRows и HtmlTableCells не отображается в таблице. Мне удалось успешно заполнить интерфейсную таблицу на другой странице asp в том же проекте (Images.aspx), но она не будет работать в этом (Products.aspx).
внешние таблицы выглядят так:
Images.aspx
<table id="tblImages" border="1" style="padding:20px;" runat="server"></table>
Products.aspx
<table id="tblProducts" border="1" style="padding:20px;" runat="server"></table>
Вот код C# с каждой страницы:
Page_Load в Images.aspx.cs (работает)
List<Product> productList = DBL.GetProductList(); // Creates a list of products using a database table
foreach (Product product in productList)
{
HtmlTableRow tRow = new HtmlTableRow();
tblImages.Rows.Add(tRow);
for (int i = 0; i <= 1; i++)
{
HtmlTableCell tCell = new HtmlTableCell();
if (i == 0)
{
tCell.InnerText = product.Name;
}
else if (i == 1)
{
HtmlButton btn = new HtmlButton();
btn.ID = product.ModelPath;
btn.Attributes["class"] = "view";
btn.Attributes["type"] = "button";
btn.InnerText = "View Model";
tCell.Controls.Add(btn);
}
tRow.Cells.Add(tCell);
}
}
Page_Load в Products.aspx.cs (не работает)
List<Product> productList = DBL.GetProductList(); // Creates a list of products using a database table
int column = 1; // keeps track of the column of the cell being populated
const int TOTAL_COLUMNS = 3; // number of columns in the table
HtmlTableRow tRow = new HtmlTableRow();
tblProducts.Rows.Add(tRow);
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
tCell.Attributes["colspan"] = "3";
tCell.InnerText = "Products";
foreach (Product product in productList) // for every product in the list
{
if (column == 1) // Create and add a new row if on the first column
{
tRow = new HtmlTableRow();
tblProducts.Rows.Add(tRow);
}
// Create cell and populate it
tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
tCell.InnerHtml += "<h1>" + product.Name + "</h1>" +
"<ul><li>$" + product.Cost + "</li>" +
"<li>" + product.Description + "</li></ul>";
if (column == TOTAL_COLUMNS) // if the final column has been reached, reset column counter
{
column = 1;
}
else // Otherwise, increment column counter
{
column++;
}
}
if (column != 0) // Adds empty cells if the end of the row wasn't reached in the foreach loop
{
for (int i = column; i < TOTAL_COLUMNS; i++)
{
tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
}
}
Я попытался скопировать C# код из Images.aspx.cs в Products.aspx.cs, чтобы увидеть, было ли это что-то с кодом в Products, но таблица все равно не будет заполнена.