oscilatingcretin
oscilatingcretin

Reputation: 10959

ASP.NET: Is it possible to inject CSS or Javascript into every page in my web app?

I am working on a web application that uses a frameset instead of a master page. I want to set up the app so that the background color of all pages is a light shade of red whenever I am debugging locally with a production connection string.

I came across one question here on StackOverflow where someone suggested creating a base page from which all page inherit, but I need a dynamic approach that just works without having to edit every page class in the application. Switching over to use master pages at this point is not an option.

Is there a way to use global.asax or some other means to inject a style or script tag into the head of every document that loads?

Upvotes: 1

Views: 877

Answers (2)

oscilatingcretin
oscilatingcretin

Reputation: 10959

Wooter wooter!

Private Sub Global_asax_PreRequestHandlerExecute(sender As Object, e As System.EventArgs) Handles Me.PreRequestHandlerExecute

    Dim p As Page = TryCast(System.Web.HttpContext.Current.Handler, Page)

    If Not p Is Nothing Then p.ClientScript.RegisterStartupScript(p.GetType, Guid.NewGuid.ToString("n"), "document.body.style.backgroundColor = 'red';", True)

End Sub

The key is to grab the page reference and then assign a startup script to it to add a style to the body element. Basically, once you know how to get a reference to the page object, the sky's the limit. Of course, a master page is always recommended for ground-up development, but this should get you some quick and dirty functionality if you ever need it.

Upvotes: 2

Bex
Bex

Reputation: 4928

I posted a question a while ago asking how to force all pages to inhertit froma particular base page. The answer came to late for me so I never tried it but check out this post and some one said it should work.

The page it is pointing to is: http://ryanfarley.com/blog/archive/2004/06/08/766.aspx

Upvotes: 0

Related Questions