简体   繁体   English

合并用户控件中的pageLoad()代码?

[英]Combine pageLoad() code from user control?

I have main default.aspx page which used to have the following Javascript in it: 我有一个主要的default.aspx页面,该页面过去包含以下Javascript

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: 现在,尽管我有一个新的用户控件UserControlFavoriteUpdate.ascx并希望其中包含以下内容,以便任何嵌入了用户控件的页面也都包含该Javascript

  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. 看来,如果调用页面最终在其Javascript列出了两个pageLoad()函数,则它只会运行最后一个。

How can I combine the user-control's pageLoad() code into the calling page's pageLoad() ? 如何将用户控件的pageLoad()代码合并到调用页面的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. 在最坏的情况下,我可以手动将该行放入调用页面代码中,但希望将其全部保留在UserControlFavoriteUpdate.ascx中,以防其他人将该控件嵌入到页面中而没有意识到它需要pageLoad()代码。

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. 考虑在用户控件背面的Page_Load()方法中添加加载脚本,以避免在同一范围内出现重复的方法。

Something like: 就像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM