简体   繁体   中英

How do I execute javascript in a dynamically-added User Control

I have a GridView of thumbnail photos within an UpdatePanel, with clickable icons in each row which each load a User Control containing a small Google Map into the relevant row. The idea is that the user can geotag (by clicking on the map) any photo in the GridView.

In the map User Control, there is some Javascript initialising the map:

function initializeMap() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById('Map'), {size: new
GSize(336, 200)});
        // set the map controls, set the centre, etc.
    }
}

Problem is: I don't know how to call the initializeMap() method. Because it's dynamically added to the page, any attempt to link it to the calling button by "onClientClick=initializeMap()" for example, gives a JS error, saying the method isn't defined.

I've tried

ClientScript.RegisterStartupScript(this.GetType(), "initializeMap", "<script type=\"text/javascript\">alert('Here'); initializeMap();</script>");

but I don't think I'm on the right track with that one either.

您应该使用ScriptManager发出包含涉及UpdatePanel的javascript,如下所示:

ScriptManager.RegisterStartupScript(this, this.GetType(), "initializeMap", "initializeMap();", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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