我無法創建表並將從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();
}
}
}
}
}
顯然我有點綠色的這一切。基本上,它會從數據庫中提取所有的解決方案標題,我希望標題可以放在他們自己的單元格中,我將繼續將它們鏈接到相應的頁面。希望這一切都有道理,如果不是,我很樂意提供更多信息。