简体   繁体   English

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

[英]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.

相关问题 如何根据用户输入动态添加用户控件? - How do I dynamically add a user control based on user input? 如何处理来自用户选项卡中的控件的事件,该用户控件是从选项卡页派生的,该选项卡在运行时动态添加到选项卡控件? - How do I handle events from controls inside a User Control derived from a Tab Page meant to be added dynamically to a Tab Control at run time? 查找动态添加的用户控件 - Find dynamically added user control 使用jQuery将用户控件动态加载到div后,如何使javascript执行? - How to make javascript execute after using jQuery to dynamically load user control to div? 回发后处理动态添加的按钮上的事件 - Handling an event on a dynamically-added Button after postback 如何在stackpanel中为动态添加的用户控件设置高度? - How to set height to the dynamically added user control in stackpanel? 如何从动态添加的用户控件访问值? - How to access values from dynamically added user control? 如何在动态添加的用户控件内为LinkBut​​ton添加触发器 - How to add a Trigger for LinkButton inside of dynamically added user control 如何控制动态添加的usercontrol? - How to control that dynamically added usercontrol? 在回发时保持动态添加用户控件的状态? - Maintain the state of dynamically added user control on postback?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM