且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在用户控件中公开ContentPlaceHolder

更新时间:2022-06-20 08:08:20

在用户控件标记中,您可以将占位符与所需的所有css和js引用放在一起,像这样:

In the user control markup you can put place holder with all css and js references you need like this:

<asp:PlaceHolder ID="phStyles" runat="server">
  <link type="text/css" rel="stylesheet" href="/App_Themes/Default/style/MyStyles.css" />
</asp:PlaceHolder>

除了后面的用户控件代码外,您还可以将占位符添加到页面控件中:

Than in the user control's code behind you can add the place holder to the page's controls colection:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Header.Controls.Add(phStyles);
    }

唯一的条件是head标签必须在Page上具有runat ="server",如下所示:

The only condition is that on the Page the head tag must have runat="server" like this:

<head runat="server">
</head>