繁体   English   中英

onOnClientClick在工作,但onClick在asp.net上不工作

[英]onOnClientClick is working but onClick is not working on asp.net

我的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函数。 我该怎么办? 预先感谢您的见解。

妮莎干杯

触发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

希望对您有所帮助,让我知道!

因此,在@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>");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM