I am using ASP.NET and C#:
I have data I wish to display in HTML elements on my webpage. 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 . Below is where I created my div and paragraphs in my code behind
//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.
<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. 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("..")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.