user1946932
user1946932

Reputation: 602

Combine pageLoad() code from user control?

I have main default.aspx page which used to have the following Javascript in it:

function pageLoad()
    {
        fUpdateFavoriteImageIfNeeded();
        fJGPSSet();          
    }

Now though I have a new user control UserControlFavoriteUpdate.ascx and want to have the following in it so that any page that has the user control control embedded in it also gets that Javascript included:

  function pageLoad()
        {
            fUpdateFavoriteImageIfNeeded();                         
        }

It seems that if the calling page ends up having two pageLoad() functions listed in it's Javascript it only runs the last one.

How can I combine the user-control's pageLoad() code into the calling page's pageLoad()? In the worst case I can manually put that line in the calling page code but was hoping to keep it all within the UserControlFavoriteUpdate.ascx in case someone else embedded that control in a page and didn't realize it needed that pageLoad() code.

Upvotes: 0

Views: 99

Answers (1)

Daniel Forslund
Daniel Forslund

Reputation: 241

Consider adding a load script in the Page_Load() method in the back page of your user control to avoid duplicate methods within the same scope.

Something like:

Page.ClientScript.RegisterStartupScript(
    Me.[GetType](), 
    "LoadScript", 
    "Sys.Application.add_load(function() { fUpdateFavoriteImageIfNeeded(); });", 
    True);

Upvotes: 2

Related Questions