我有以下模板用我的UL
填充从DataTable收到的数据:
DataTable dt = getData(); //Insert your datasource here
foreach(DataRow row in dt.Rows){
HtmlGenericControl li = new HtmlGenericControl("li");
li.Attributes.Add("data-trait-id", row["TraitID"].ToString());
HtmlAnchor a = new HtmlAnchor();
a.Attributes.Add("data-trait-id", row["TraitID"].ToString());
HtmlGenericControl span1 = new HtmlGenericControl("span");
span1.Attributes.Add("class", "name");
span1.InnerText = row["Name"].ToString();
a.Controls.Add(span1);
HtmlGenericControl span2 = new HtmlGenericControl("span");
span2.Attributes.Add("class", "count");
span2.InnerText = row["Count"].ToString();
a.Controls.Add(span2);
li.Controls.Add(a);
ulSpecialty_selector.Controls.Add(li);
}
但是在我的页面中,我使用DataSet从SQL查询中获取列:
protected void Page_Load(object sender, EventArgs e)
{
using (OleDbConnection connection = new OleDbConnection("Provider=MSDataShape;Data Provider=SQLOLEDB;" + "Data Source=svr;Initial Catalog=db;User ID=zh;Password=zha")) {
OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT * FROM [db].[dbo].[BookingTable]} ", connection);
DataSet dsLocation = new DataSet();
adapter.Fill(dsLocation, "Location");
}
}
如何使用第一个代码块来处理第二个代码块,以便在LI
内生成UL
?
我希望效仿以下内容:
<ul class="ulLocation" id="ulLocation2_selector" runat="server">
<li class="liSubLocation active" data-trait-id="9">
<a href="/locations/new-york/neighborhoods?tags[]=12&tags[]=66" class="premote trait-link large btn" data-trait-id="9">
<span class="check"><i class="icon icon-ok"></i></span>
<span class="name">New Rochelle</span>
<span class="count">6</span>
</a>
</li>
</ul>
答案 0 :(得分:3)
您可以轻松地将DataTable转换为DataSet,反之亦然 来自DataSet的DataTable
//Assuming oDS is my DataSet
DataTable oDt = oDS.Tables[0];// If you know name of datatable you may use oDS.Tables["name"]
DataTable的数据集
//Assuming oDT is your DataTable
DataSet oDs = new DataSet();
oDs.Tables.Add(oDT);
答案 1 :(得分:1)
假设您从GetData()返回数据集,您可以更改以下内容:
DataSet ds = getData();
DataTable dt;
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
foreach(DataRow row in dt.Rows){
...