如何在动态添加的用户控件中执行 javascript

发布于 2024-08-19 16:06:16 字数 786 浏览 10 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2024-08-26 16:06:16

您应该使用 ScriptManager 在存在UpdatePanel 涉及如下:

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

You should be using the ScriptManager to emit your javascript where there is an UpdatePanel involved like so:

ScriptManager.RegisterStartupScript(this, this.GetType(), "initializeMap", "initializeMap();", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文