我目前正在尝试创建一个网站,当使用下拉列表选择某个Make,Model,Year时,该网站会动态显示项目。使用利用HtmlAgilityPack的网络爬行检索项目信息。
所以我目前有一个数据库表,其中包含数千个包含列的项: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
}
我已经在这一步上工作了一段时间所以我想我会发帖,看看是否有人有任何提示或类似的例子,而我自己继续尝试解决它。任何事都会非常感激。干杯!
答案 0 :(得分:1)
我想通了,而且很容易。
继承人我的功能是如何设置的
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);
}
}