在用户控件中调用javascript函数 [英] calling javascript function in user control

查看:104
本文介绍了在用户控件中调用javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户控制页面中显示了带有地理编码的google地图.

地图未显示地址-

请参阅以下代码:

I am showing a google map with geo coding in my usercontrol page.

The map is not displaying the address -

see the following code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChangeLocation.ascx.cs" Inherits="onlocal.UserControls.ChangeLocation" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<link href="../CSS/common.css" rel="stylesheet" type="text/css" />


 <script src="http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=ABQIAAAAvpd8mjNgroIYlldQP4fM-hREifw3z0CLY54LRc8ZC14v-wuusRQrD9AaPPOw_rxAgdi_bPu9S-lQxg" type="text/javascript"></script>
    <script type="text/javascript">

    var map = null;
    var geocoder = null;
    function initialize() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();
      }
    }

    function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
    alert('test link');
    initialize();
    alert('test link2');
    </script>

<cc1:ModalPopupExtender ID="ModalPopupChangeLocation" BehaviorID="ModalPopupChangeLocation"

    runat="server" BackgroundCssClass="modalBackground" TargetControlID="lnkDummy"

    PopupControlID="pnlModalPopupChangeLocation" CancelControlID="imgBtnCancel">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="lnkDummy" runat="server"></asp:LinkButton>
<asp:Panel ID="pnlModalPopupchangelocation" runat="server" Style="display: none;">
<asp:UpdatePanel ID="updatechangelocation" runat="server">
        <ContentTemplate>
            <div style="width: 430px; height: 230px; overflow: hidden;" class="content" >
                <div class="blueNavPopUp" style="width: 430px;">
                    <div class="blueNavTextPopUp">
                        <asp:Label ID="lblTitle" runat="server" class="whiteTabText">Change
            Location</asp:Label></b></div>
                    <div class="collaspebtmPopUp">
                        <asp:ImageButton ID="imgBtnCancel" runat="server" ImageUrl="~/images/CloesImg.gif" OnClientClick="javascript:$find('ModalPopupChangeLocation').hide(); return false;"

                            alt="" /></div>
                </div>
            <p>
        <input type="text" size="60" name="address" value="1600 Amphitheatre Pky, Mountain View, CA" />
        <input type="submit" value="Go!" onclick="initialize(); return false;" />
      </p>
      <div id="map_canvas" style="width: 500px; height: 300px; background-color:Aqua;"></div>
</div>
        </ContentTemplate>
    </asp:UpdatePanel>
&nbsp;</asp:Panel><p>
    </p>

推荐答案

find('ModalPopupChangeLocation').hide();返回false;" alt =" " / < > < /div > < p > < 输入 =" 文本" 大小 60" 名称 =" 地址" 加利福尼亚州山景城的1600 Amphitheatre Pky" / > < 输入 =" 提交" 开始!" onclick =" initialize();返回false;" / > < /p > < div =" map_canvas" 样式 宽度:500像素;高度:300px; background-color:Aqua;" < /div > < /div > < /ContentTemplate > < /asp:UpdatePanel > & nbsp; < /asp:Panel > < p > < /p >
find('ModalPopupChangeLocation').hide(); return false;" alt="" /></div> </div> <p> <input type="text" size="60" name="address" value="1600 Amphitheatre Pky, Mountain View, CA" /> <input type="submit" value="Go!" onclick="initialize(); return false;" /> </p> <div id="map_canvas" style="width: 500px; height: 300px; background-color:Aqua;"></div> </div> </ContentTemplate> </asp:UpdatePanel> &nbsp;</asp:Panel><p> </p>


在提交"按钮中,您正在调用 initialize ();.您是否不应该调用 showAddress ();
In your submit button, you are calling initialize(); Shouldn''t you be calling showAddress();


您所使用的上下文中的"this"关键字将引用我认为的文档?同样,在Firebug中,您可以确定此

基本上,this.address不存在.您没有给对象一个ID,而只是一个名字.当您使用ASP.Net控件时,我确定.Net还是会破坏所有ID ...例如

''myControl_1_address''

您仍然应该为您的元素提供一个ID,例如

<输入type ="text" size ="60" id =地址" name =地址" value ="1600圆形露天剧场Pky,加利福尼亚州山景城"/>

你可以试试

https://developer.mozilla.org/en/document.getElementsByName [
The ''this'' keyword within the context you are using will refer to the document I think? Again, in Firebug you can determine this

Basically, this.address doesn''t exist. You haven''t given the object an ID, only a name. When you use use ASP.Net controls, I''m sure .Net mangles all the IDs anyway...e.g

''myControl_1_address''

you should still give your element an ID anyway, e.g

<input type="text" size="60" id="address" name="address" value="1600 Amphitheatre Pky, Mountain View, CA" />

You can try

https://developer.mozilla.org/en/document.getElementsByName[^]

e.g
var address = document.getElementsByName(''address'');

That''s the last help I''m giving you on this! Between using Firebug effectively and finding your element on the page, you will be able to figure this out if you try


这篇关于在用户控件中调用javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆