我知道当你知道你在添加什么的时候,这很容易。例如,使用UI控件时,它的
Page.Header.Controls.Add(控制)
但是,我正在从CMS数据库中提取HTML代码。换句话说,我需要向母版页的Head部分添加一个字符串。
发布于 2014-04-08 20:31:13
您可以通过使用与您在问题中显示的代码类似的代码将一个Literal控件添加到标题中:
public partial class MyUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var lit = new LiteralControl();
lit.Text = "<link href=\"test.css\" rel=\"stylesheet\" />";
Page.Header.Controls.Add(lit);
}
}发布于 2014-04-08 20:25:54
如果使用的是母版页,则可以:
母版页标记:
<html>
<head runat="server">
<asp:ContentPlaceHolder ID="MyHeadContentPlaceHolder" runat="server"></asp:ContentPlaceHolder>
</head>
</html>内容页标记:
<asp:Content ID="MyContent" ContentPlaceHolderID="MyHeadContentPlaceHolder" runat="server">
<asp:Literal ID="MyLiteralForCmsContent" runat="server"></asp:Literal>
</asp:Content>内容页代码-隐藏:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Load the literal with content from the CMS.
this.MyLiteralForCmsContent.Text = ""; //Hook up the call to the CMS here.
}
}https://stackoverflow.com/questions/22946564
复制相似问题