修改用户控件中的页眉
如何从嵌入的用户控件中修改页面的头部? 我知道我可以在 .aspx 页面的头部运行该控件,但我有一个现有网站,其中包含许多我不想更改的页面。 它们的共同点之一是 menubar.ascx。 因此,我想我可以将代码放在那里来修改包含页面的 head 元素,但没有骰子。 我试图实现的代码看起来像这样,但是,Page.Header 为空。
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim favicon As New HtmlLink favicon.Attributes.Add("REL", "SHORTCUT ICON") favicon.Attributes.Add("HREF", "images/bh_favicon.ico") Page.Header.Controls.Add(favicon) End Sub
我尝试将其放入 PreRender 和 Render 事件中,但还是一样。 Page.Parent.Page.Header 也为空。 有没有更好的方法来做我想做的事? 最终我想向一组页面添加一个与默认图标不同的图标。 基本上我在同一个代码库中有两个网站。
客气一点,这是我的第一篇文章。
TIA
How can I modify the head portion of a page from within an embeded user control? I know I can have the control run in the head portion of the .aspx page but I have a existing site with numerous pages that I don't want to change. One thing they all have in common is the menubar.ascx. So, I figured I could put the code there to modify the head element of the containing page, but no dice. The code I am trying to implement looks like this, however, the Page.Header is null.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim favicon As New HtmlLink favicon.Attributes.Add("REL", "SHORTCUT ICON") favicon.Attributes.Add("HREF", "images/bh_favicon.ico") Page.Header.Controls.Add(favicon) End Sub
I tried putting it in the PreRender and the Render events but same thing. The Page.Parent.Page.Header is null too. Is there a better way to do what I want to do? Utlimately I want to add a favicon to a group of pages that is different from the default favicon. Basically I have two sites on in the same code base.
Be nice, this is my first post.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能需要使页面头在服务器上运行,以便用户控件可以看到它。
例如:
如果您的所有页面上还没有这样做,我想这有点违背了这一点。 也许解决方案范围广泛的正则表达式搜索/替换将是为了实现这一点。
You may need to make your Page Head run at server, so the usercontrol can see it.
eg:
Which I guess sort of defeats the point if this isn't already done on all your pages. Maybe a solution wide RegEx search/replace would be in order to implement this.
正如 @Program.X 所提到的,您可能需要完整的搜索/替换。 如果您打算这样做,您可能想更进一步并使用母版页,但这实际上取决于您的时间限制以及需要修改的页面数量。
As mentioned by @Program.X you may need a full search/replace. If you are going to do that you might want to go one step further and use a Master page, but it really depends on your time constraints and how many pages there are to modify.
感谢您的回答。 我知道我要求的是工作量最少的解决方案,但是,我想让代码易于管理。 我认为我要做的是构建一个母版页作为所有页面的模板(就像 @devstuff 建议的那样)。 然后我将更改现有的页面(大约 50 个页面)以使用母版页。 这样,如果将来出现类似的情况,我可以轻松地在一处更改所有内容。
谢谢你的帮助!
Thanks for your answers. I know I was asking for the least amount of work solution, however, I want to make the code easy for me to manage. I think what I am going to do is construct a master page as a template for all pages (like @devstuff suggested). Then I am going to change the existing pages, about 50 pages, to use the master page. That way if something like this pops in the future I can easily change everything in one place.
Thanks for you help!