views:

227

answers:

3

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

+1  A: 

You may need to make your Page Head run at server, so the usercontrol can see it.

eg:

<head runat="server">

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
This will be my backup solution if the customer does not want to budget the time for the master page solution. You're right, a simple find/replace using a regEx would fix this. Thanks!
JKC
A: 

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
This is going to be quite the undertaking, but I like the concept.
JKC
+1  A: 

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!

JKC
I'd definitely recommend using Master pages for all new sites that'll end up with more than, say, 3 pages. Saves a lot of pain later, since most content pages don't really care about framing stuff like logos, menus, etc.
devstuff