【问题标题】:Dynamic HTML Elements using data from a Database使用来自数据库的数据的动态 HTML 元素
【发布时间】:2017-04-24 20:29:34
【问题描述】:

我目前正在尝试创建一个网站,当使用下拉列表选择某个品牌、型号、年份时动态显示项目。使用 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
}

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

【问题讨论】:

    标签: javascript c# jquery html css


    【解决方案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);
            }
        }
    

    现在它是如何显示的,我只需要弄清楚如何将它们放入&lt;div&gt;s 以便我可以设置它们的样式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多