C# Register onUnload JavaScript programmatically in code-behind

by Brad 3. June 2008 14:22

Having just created a Google Map server control I spent some time trying to register the relevant unload scripts that the Google API demands. However there was no easy way to get access to the body tag, and I certainly didn't want to have to edit the body tag, adding runat="server" as that would ruin my nicely "wrapped" control, I simply want a developer to drag it out of their Visual Studio Toolbox and it work...

So after some thought I tried the following, which worked a treat:

//Now we need to add the google maps 
//ONUNLOAD JavaScript function (it its not be added already)
if (!Page.ClientScript.IsStartupScriptRegistered(
                 typeof(MyControl), "GoogleMapUnload"))
{
    Page.ClientScript.RegisterStartupScript(
        typeof(MyControl), 
        "GoogleMapUnload", 
        "window.onunload = function () { GUnload(); };", 
        true); 
}

So simply replace the GUnload(); with your own custom JavaScript - you get the idea....

Tags:

C#

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar