我无法创建表并将从SQL中提取的数据放入其中。我现在的解决方案是在Default.aspx中创建一个表格,然后在C#代码中创建行和单元格。为.NET代码隐藏创建一个动态表C#
我的aspx代码:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Select a topic to view.
</h2>
<asp:Table ID="solutions" runat="server" Width="100%">
</asp:Table>
</asp:Content>
而且,所有的行动应该,但不会发生: C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HtmlAgilityPack;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection anca = new System.Data.SqlClient.SqlConnection();
anca.ConnectionString = "Data Source=anca;Initial Catalog=servicedesk;
anca.Open();
SqlCommand sub = new SqlCommand("SELECT TITLE FROM dbo.Solution", anca);
SqlDataReader sReader = sub.ExecuteReader();
List<String> subject = new List<string>();
int solCount = 0;
while (sReader.Read())
{
subject.Add(sReader.ToString());
solCount++;
}
sReader.Close();
TableRow newRow = new TableRow();
TableCell newcell = new TableCell();
int adder = 0;
while (adder <= solCount)
{
solutions.Rows.Add(newRow);
for (int i = 0; i <= 6; i++)
{
newRow.Cells.Add(newcell);
newcell.Text = subject[adder].ToString();
}
}
}
}
}
显然我有点绿色的这一切。基本上,它会从数据库中提取所有的解决方案标题,我希望标题可以放在他们自己的单元格中,我将继续将它们链接到相应的页面。希望这一切都有道理,如果不是,我很乐意提供更多信息。