在用户控件中公开ContentPlaceHolder [英] Expose ContentPlaceHolder in User Control
问题描述
我正在实现一个Web应用程序,但遇到以下问题:
I am implementing a web application and i faced the following problem:
我有一个带有其自己的样式和js文件的用户控件,当我将用户控件添加到页面中并尝试验证murkup时,由于用户控件的样式和js位于主体中,因此我得到了错误页面而不是头部.我想公开页面标题,以便从我的Web应用程序中添加用户控件,并将样式和js文件直接添加到页面标题中.你有什么建议吗?我还没有在线获得任何好的解决方案
I have a User control with it's own styles and js files, when i add the user control to the page and i try to validate the murkup i get error due to the fact that the user control's styles and js are in the body of the page in stead of the head. i would like to expose the page head to add from my web application user controls the styles and js files directly into the page head. have you got any suggestion? i did not get any good solution online yet
预先感谢
推荐答案
在用户控件标记中,您可以将占位符与所需的所有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>
这篇关于在用户控件中公开ContentPlaceHolder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!