[英]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. 我在UpdatePanel中有一个缩略图照片的GridView,每行中都有可单击的图标,每个图标都将包含一个小的Google Map的用户控件加载到相关行中。 The idea is that the user can geotag (by clicking on the map) any photo in the GridView. 这个想法是用户可以在GridView中对任何照片进行地理标记(通过单击地图)。
In the map User Control, there is some Javascript initialising the map: 在地图用户控件中,有一些Javascript可以初始化地图:
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. 问题是:我不知道如何调用initializeMap()
方法。 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. 因为它是动态添加到页面的,所以例如通过"onClientClick=initializeMap()"
将其链接到调用按钮的任何尝试都会产生JS错误,表示未定义该方法。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.