inserting analytics code with asp.net into page - asp.net

I have about 50 sites that currently use a template code base to run. There are 3 master pages in the site and when we built them we didn't think to put a literal control on the page to plug in the site's google analytics code. Now I have to add the analytics code right before the closing body tag of the page and if I add a literal to the master page and push it to the web sites it will overwrite all the customizing that we've done on those sites. So, rather than changing the master page.aspx, I'd like to just write it to the page from the code behind if possible, but I can't figure out how. I've seen using the ClientScript to register javascript on the page, but is there a way to programmatically insert a literal as the last control on the page and set it's content to the analytics code? Thanks.

You can use the HtmlGenericControl:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol(v=VS.80).aspx
Use the InnerHtml property to inject your HTML.

Related

Finding master page control from within iFrame

Scenario:
I have one main master page say MasterPage1. In that master page I have a splitter. In that splitter there is an iframe. Within that iframe we load another master page say "MasterPage2". In MasterPage2 we load a page on which different User Controls are rendered.
Problem:
Now I want to find a control on MasterPage1 from my User Control loaded on the page in MasterPage2.
Please help....
Problem To your Scenario:
masterpages and content pages are rendered as a single object, thus the page class is able to reference every element found in both the objects(master and content page). When you are rendering an iframe the iframe content is requested by client hence no reference exists. so it is not possible to reference each other on server.
Solution to the problem
From above you must have realized all the problem is the reference , so you will have to hack inti it. the simplest way I can think is to use querystring.
call the iframe page with querystring containing a identifier to the masterpage like mpage=mpage1,mpage=mpage2 etc.
Now in masterpage2 request the querystring to find which masterpage is applied and proceed. This way you will have little relaxaction because masterpage1 content cannot be changed but masterpage2 can be.
Now you will need to work more to what you need. Proceed only if this is the only way to solve the real problem(I think the problem is not masterpage but the solution to the problem that is making you to do these weired things).
Well for that you will have to use javascript and handlers which will render and return the rendered usercontrol. But i seriously say not to use this setup in production and find other alternative by changing your code to use usercontrol instead of iframe.

Put Script Manager in master page

is it right to put script manager in master page ?
It isn't wrong, as such.
Really depends on your requirements.
If you need to use scripts in several pages that use the same master page, use ScriptManager in the master page.
Just keep in mind that you can only have one ScriptManager in the whole loaded page, so you can't add one in content pages as well.
See this article on MSDN for an example (Using the ASP.NET UpdatePanel Control with Master Pages).

Run a script inside a content page. ASP.NET

I have a masterpage and content page. And I'm trying to run a java script that needs to be executed on the page loading.
As I am using a master page do not have access to the field
My doubt is how to run the script within the content page? And where the script has to be? the head of the master page or inside the content page?
You can run C#/VB code on page load at any page or custom user control, so you will have access to whatever on the page.
I think you want to put that code in masterpage to standardize the procedure, one of the solutions (I don't know if you can accomblish this):
you can build your own "CustomPage" class by inheriting from "Page"
so, you can set the onLoadEvent to a preset action.
and inherit from it for all your pages.
You could put a ContentPlaceholder in the Head section of your master page. Thats if you want to put your script in the Head

Web User Controls, Javascript and Script Managers

Is there a way to only add a script manager to your Web User Control if there is not one already on the page (on the main page or if you are using the Web User Control multiple times)?
Same question about adding javascript files to the page, many times I have controls that add the javascript into the head of the page for the control.
Regarding ScriptManager:
I would use master pages, and include the script manager on your master page. Alternatively, if you have something like Header.ascx which you know is included on every page, you could put it there also.
Regarding javascript files:
Use the ClientScriptManager.RegisterStartupScript method to include javascript on your page. It will not produce duplicates if they share the same key name parameter.
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

ASP.NET master pages order of adding scripts

I have a master page that adds the jquery library via a registerclientscriptinclude:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"JQuery",
Page.ResolveUrl("~/Scripts/jquery-1.2.6.min.js"));
In a page that uses that master page I want to include a script that depends on jquery:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"jqplugin1",
Page.ResolveUrl("~/Scripts/jquery.plugin1.compressed.js"));
This causes a problem because the page's client scripts are written first and then the master pages client scripts (causing the plugin to complain that "jquery" is undefined).
Is there anyway to control the ordering of client script includes? Alternatively any recommendation on how best to control this (temporarily the master page is just including everything... but that's not going to work long term).
since master page is like a control embedded in a page, you can add these scripts towards the end of the page cycle, the control would fire first and then page, so your page would be fine.
You could re-include the jQuery library in the page, if it's already there it will simply override it, if not it will be added.

Resources