如何从后面的代码中定位.aspx文件中的HTML元素

时间:2015-07-06 14:04:36

标签: c# html asp.net datatable datarow

我正在使用ASP.NET和C#:

我希望在我的网页上的HTML元素中显示数据。目前,我可以通过在后面的代码中创建一个新的div和段落来显示数据,但不能将其作为现有HTML代码的目标。下面是我在我的代码中创建div和段落的地方

      //Populating a DataTable from database.
      DataTable dt = this.GetData();

      //Building an HTML string.
      StringBuilder html = new StringBuilder();

      //Div start.
      html.Append("<div>");                

      //Building the Data rows.
      foreach (DataRow row in dt.Rows)
            {
                html.Append("<div>");
                foreach (DataColumn column in dt.Columns)
                {
                    html.Append("<p>");
                    html.Append(row[column.ColumnName]);
                    html.Append("</p>");
                }
                html.Append("</div>");
            }

            //Div end.
            html.Append("</div>");

如何定位现有的HTML元素以显示我的数据。

    <div class="deal-info">             
         <h3>Name</h3>
         <p>Description</p>
         <p>Location</p>
         <p>Price</p>
    </div>  

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

如果将其设置为runat服务器,基本上您可以访问代码隐藏中的每个HTML对象。无论我是否会推荐它,它都是如何工作的。

static void FullDirList(
    DirectoryInfo dir, string searchPattern, string excludeFolders, int maxSz, int depth)
{
    var results =
        from fi in GetFullDirList(dir, searchPattern, depth)
        where String.IsNullOrEmpty(excludeFolders)
            || !Regex.IsMatch(fi.FullName, excludeFolders, RegexOptions.IgnoreCase)
        group fi.FullName by fi.Directory.FullName;

    var directoriesFound = results.Count();
    var filesFound = results.SelectMany(fi => fi).Count();

    var aggregateByLength =
        results
            .SelectMany(fi => fi)
            .Aggregate(new [] { new StringBuilder() }.ToList(),
                (sbs, s) =>
                {
                    var nl = s + Environment.NewLine;
                    if (sbs.Last().Length + nl.Length > maxSz)
                    {
                        sbs.Add(new StringBuilder(nl));
                    }
                    else
                    {
                        sbs.Last().Append(nl);
                    }
                    return sbs;
                });

    foreach (var sb in aggregateByLength)
    {
        File.WriteAllText(nextOutPutFile(), sb.ToString());
    }
}

<div class="deal-info">             
     <h3>Name</h3>
     <p>Description</p>
     <p>Location</p>
     <p>Price</p>
</div>
<asp:PlaceHolder ID = 'yourplaceholder' runat = 'server' />

System.Text.StringBuilder html = new System.Text.StringBuilder();
//Any code...
//html.Append("..")
yourplaceholder.Controls.Add(new System.Web.UI.LiteralControl(html.ToString()));