如何在动态添加的用户控件中执行 javascript
我在 UpdatePanel 中有一个缩略图照片的 GridView,每行都有可单击的图标,每个图标将包含一个小型 Google 地图的用户控件加载到相关行中。这个想法是用户可以对 GridView 中的任何照片进行地理标记(通过单击地图)。
在地图用户控件中,有一些 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.
}
}
问题是:我不知道如何调用 initializeMap()
方法。因为它是动态添加到页面的,所以任何尝试通过 "onClientClick=initializeMap()"
将其链接到调用按钮的行为都会给出 JS 错误,指出该方法未定义。
我已经尝试过
ClientScript.RegisterStartupScript(this.GetType(), "initializeMap", "<script type=\"text/javascript\">alert('Here'); initializeMap();</script>");
,但我也不认为我走在正确的道路上。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 ScriptManager 在存在UpdatePanel 涉及如下:
You should be using the ScriptManager to emit your javascript where there is an UpdatePanel involved like so: