0

我的 javascript 上有这段代码:

         function getListOfPOI() {
            geocoder = new google.maps.Geocoder();
            var address = document.getElementById('tbCity').value;

            geocoder.geocode({ 'address': address }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    var request = {
                        location: results[0].geometry.location,
                        radius: 8000,
                        types: all //['store']
                    };
                    infowindow = new google.maps.InfoWindow();
                    var service = new google.maps.places.PlacesService(map);
                    service.nearbySearch(request, callback);
                } else {
                    alert('Geocode was not successful for the following reason: ' + status);
                }
            });

            return true;
        }

我用下面的按钮调用这个函数:

<asp:Button ID="btnAddCity" Height="20px" Text="Add" runat="server" OnClientClick="return getListOfPOI();" OnClick="btnAddCity_Click" UseSubmitBehavior="false"    />

OnClientClick 完美运行,但未触发 OnClick 函数。我应该怎么办?提前感谢您的见解。

干杯,妮莎

4

2 回答 2

1

另一种触发 JavaScript 代码 + 服务器代码的方法是执行以下操作,我们使用 ClientScript.RegisterStartupScript 并调用我们的 JavaScript 函数!

在您的 .ASPX 页面中:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function clientTestClick() {
            document.write("Test!");
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="TestButton" Text="Test Me" UseSubmitBehavior="false" OnClick="TestButton_Click" runat="server"/>
    </div>
    </form>
</body>
</html>

取决于语言背后的代码..

C#:

protected void TestButton_Click(object sender, EventArgs e)
{
    ClientScript.RegisterStartupScript(Page.GetType, "JavaScript", "clientTestClick();", true);

    /*  PUT YOUR SERVER CODE HERE */
}

VB.NET:

Protected Sub TestButton_Click(sender As Object, e As EventArgs) Handles TestButton.Click

    ClientScript.RegisterStartupScript(Me.GetType(), "JavaScript", "clientTestClick()",True)

    Dim x As String 
    x="hello" 'You can set a debug-point here and see that this will fire

End Sub

希望这会有所帮助,让我知道!

于 2014-03-16T22:28:16.593 回答
0

所以在@lucidgold 的帮助下,终于可以正常工作了!解决方案是在按钮服务器端调用javascript函数。调整后,这是完整的解决方案:

  1. 我删除了 JavaScript 函数的 return true
  2. 删除按钮上的 useSubmitBehaviour 属性:
    <asp:Button ID="TestButton" Text="Test Me" OnClick="TestButton_Click" runat="server"/>

  3. 在 C# 中运行 RegisterStartupScript,如下所示:
    ClientScript.RegisterStartupScript(GetType(), "script", "<script type ='text/javascript'> getListOfPOI(); </script>");

于 2014-03-17T06:39:46.267 回答