[英]How to target HTML elements in .aspx file from the code behind
I am using ASP.NET and C#: 我正在使用ASP.NET和C#:
I have data I wish to display in HTML elements on my webpage. 我有希望在网页上的HTML元素中显示的数据。 At the moment I can display the data by creating a new div and paragraphs in the code behind but cannot target it to go into my existing HTML code . 目前,我可以通过在后面的代码中创建一个新的div和段落来显示数据,但是不能将其定位到现有的HTML代码中。 Below is where I created my div and paragraphs in my code behind 下面是我在后面的代码中创建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>");
How can I target my existing HTML elements for my data to be displayed. 如何定位现有HTML元素以显示数据。
<div class="deal-info">
<h3>Name</h3>
<p>Description</p>
<p>Location</p>
<p>Price</p>
</div>
Any help is much appreciated :) 任何帮助深表感谢 :)
Basically you can access every HTML object in your codebehind if you set it as runat server. 基本上,如果将其设置为Runat服务器,则可以访问代码中的每个HTML对象。 Whether I would recommend it or not, here is how it works. 无论我是否推荐,这都是它的工作原理。
<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()));
or 要么
<div class="deal-info" id = "yourplaceholder" runat = "server">
<h3>Name</h3>
<p>Description</p>
<p>Location</p>
<p>Price</p>
</div>
System.Text.StringBuilder html = new System.Text.StringBuilder();
//Any code...
//html.Append("..")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.