[英]ASPX add HTML content programmatically
我有一個繼承自MasterPage
的簡單ASPX
頁面:
(Popup.master)
<div class="text_container">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
(Privacy.aspx)隱私
<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
</asp:Content>
我需要在Privacy.aspx.cs中添加HTML內容:
public partial class Privacy : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
ContentPlaceHolder body = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
我看到了這個示例,但是正如我看到的那樣,我需要在內部使用asp控件。 如何僅將HTML內容添加到ContentPlaceHolder1
?
ContentPlaceHolder旨在使您能夠使用Content控件在頁面標記中添加控件,並使它們出現在母版頁的上下文中。 鑒於此,您可以在BodyContent
在BodyContent
內容控件中添加通用html控件,並在后面的代碼中在添加的控件上設置InnerContent屬性。
標記
<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolder1"
runat="server">
<span id="SpanContent" runat="server"></span>
</asp:Content>
背后的代碼
protected void Page_Load(object sender, EventArgs e)
{
SpanContent.InnerHtml = "<b>Hello!</b>";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.