gpt4 book ai didi

javascript - 使用数据库中数据的动态 HTML 元素

转载 作者:行者123 更新时间:2023-11-28 15:40:06 26 4
gpt4 key购买 nike

我目前正在尝试创建一个网站,当使用下拉列表选择特定品牌、型号、年份时动态显示元素。使用使用 HtmlAgilityPack 的 Web 爬网检索元素信息。

所以我目前有一个数据库表,其中包含数千个元素,其中包含以下列:id、Make、Model、Year、PartCategory、PartUrl、ImageUrl、PartBrand、PartName、PartPrice。

我试图做的是将每个站点分成 div,这样用户就可以轻松地分辨出他们正在查看的元素来自哪个站点。示例:

<div id="siteOne">
<table>
<tr>
<td>
<img url="ImageUrl">
<a href="PartUrl">PartBrand & PartName & PartPrice</a>
</td>
</tr>
<tr>
<td>
<img url="ImageUrl">
<a href="PartUrl">PartBrand & PartName & PartPrice</a>
</td>
</tr>
</table>
</div>
<div id="siteTwo">
.............
</div>
<div id=""siteThree>
.............
</div>

我目前总共只使用了三个站点,我可以轻松地将数据拉入 DataTable,然后查看返回了多少行,因此该数字就是我需要创建的动态实例数。我很确定 Jquery 可以处理这项工作,但是我找不到任何使用 DataTable 的 DataRows 来控制动态部分的示例,通常是使用按钮事件或类似的东西。

我想代码看起来像这样:

//this foreach statement will be in the last dropdown list's SelectedIndexChanged event
//when the last dropdown is chosen, a datatable will be returned with all the items that match that particular make/model/year
foreach (DataRow tempRow in tempDataTable)
{
//this should pull data from each column one at a time
//column: 0 = id, 1 = Make, 2 = Model, 3 = Year, don't think I'll need these right now
string partCategory = tempRow[4].ToString();
string partUrl = tempRow[5].ToString();
string imageUrl = tempRow[6].ToString();
string partBrand = tempRow[7].ToString();
string partName = tempRow[8].ToString();
string partPrice = tempRow[9].ToString();

//this is where the jquery would generate the dynamic elements in the table.
//three main divs can be hard coded for now, only using 3 sites currently
//this means the <table></table> in each will most likely be hard coded as well
//the <tr></tr>, <td></td>, <img>, and <a></a> will need to be generated dynamically each loop iteration
}

我已经在这一步上努力了一段时间,所以我想我会发帖看看是否有人有任何提示或类似的例子,同时我继续尝试自己解决它。任何事情都将不胜感激。干杯!

最佳答案

我想通了,而且很简单。

我的函数是这样设置的

    protected void drpdwnYear_SelectedIndexChanged(object sender, EventArgs e)
{
//get the data for the specific make/model/year from the database
DataTable dt = new DataTable();
string tempYear = drpdwnYear.SelectedItem.ToString();
string tempManufacturer = Globals.myManufacturer;
string tempModel = Globals.myModel;
Globals.myQuery = "SELECT * FROM CrawlerInfo WHERE Model = '" + tempModel + "' AND Year ='" + tempYear + "'";
try
{
using (SqlConnection con = new SqlConnection(Globals.myConStr))
{
using (SqlCommand cmd = new SqlCommand(Globals.myQuery, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
}
}
}
}
catch (Exception ex)
{
handleTheError(ex);
}

//put the data into HTML elements
try
{
int tempflag = 0;
foreach (DataRow tempRow in dt.Rows)
{
string tempCategory = tempRow[4].ToString();
string tempPartUrl = tempRow[5].ToString();
string tempImageUrl = tempRow[6].ToString();
string tempPartBrand = tempRow[7].ToString();
string tempPartName = tempRow[8].ToString();
string tempPrice = tempRow[9].ToString();
string tempStoreName = tempRow[10].ToString();

Image tpImg = new Image();
tpImg.ID = "id_img_" + tempflag;
tpImg.ImageUrl = tempImageUrl;
Page.Controls.Add(tpImg);

HyperLink hyp = new HyperLink();
hyp.ID = "id_link_" + tempflag;
hyp.NavigateUrl = tempPartUrl;
hyp.Text = tempPartBrand + " " + tempPartName + ": " + tempPrice + "\n\n";
Page.Controls.Add(hyp);
tempflag++;
}
}
catch (Exception ex)
{
handleTheError(ex);
}
}

目前它是如何显示的,我只需要弄清楚如何将它们放入 <div> 中这样我就可以设计它们了。 Website current look

关于javascript - 使用数据库中数据的动态 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com