我已经搜索了一段时间,但永远无法得到关于正确方法的明确答案,或者即使它是可能的。我从 SQL 中的表中获取 2 行,将它们返回到 DataTable 中。我遍历行并为该行动态创建一个 div,然后为每一列使用存储的值创建一个标签,然后为下一行重复该过程。
我所缺少的只是将标签存储到一个列表中,以带回将在其中创建 div 的占位符。
这是一个片段......另外,我想这样做,而不是出于学习目的使用gridview或表格,我已经知道如何使用gridview和表格来做到这一点。我在这个 SQL 表中总共有 7 列,并且可以有无限数量的行。
编辑
public void AddDiv(DataTable gameData)
{
for (int i = 0; i < gameData.Rows.Count; i++)
{
//newControl.InnerHtml = AddLabel(gameData, i);
//PlaceHolder1.Controls.Add(newControl);
HtmlGenericControl newControl = new HtmlGenericControl("div");
newControl.ID = "div" + i++;
Label lblTitle = new Label();
lblTitle.Text = gameData.Rows[i]["Game_Title"].ToString();
this.Controls.Add(lblTitle);
PlaceHolder1.Controls.Add(lblTitle);
Label lblPublisher = new Label();
lblPublisher.Text = gameData.Rows[i]["Game_Publisher"].ToString();
this.Controls.Add(lblPublisher);
PlaceHolder1.Controls.Add(lblPublisher);
Label lblGenre = new Label();
lblGenre.Text = gameData.Rows[i]["Game_Genre"].ToString();
this.Controls.Add(lblGenre);
PlaceHolder1.Controls.Add(lblGenre);
Label lblESRB = new Label();
lblESRB.Text = gameData.Rows[i]["Game_ESRB"].ToString();
this.Controls.Add(lblESRB);
PlaceHolder1.Controls.Add(lblESRB);
Label lblUserRating = new Label();
lblUserRating.Text = gameData.Rows[i]["Game_UserRating"].ToString();
this.Controls.Add(lblUserRating);
PlaceHolder1.Controls.Add(lblUserRating);
Label lblWebsite = new Label();
lblWebsite.Text = gameData.Rows[i]["Game_Website"].ToString();
this.Controls.Add(lblWebsite);
PlaceHolder1.Controls.Add(lblWebsite);
}
}