hewstone
hewstone

Reputation: 4695

Modify Page Head in User Control

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

Upvotes: 3

Views: 2801

Answers (3)

hewstone
hewstone

Reputation: 4695

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!

Upvotes: 1

devstuff
devstuff

Reputation: 8387

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.

Upvotes: 0

Program.X
Program.X

Reputation: 7412

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.

Upvotes: 4

Related Questions