簡體   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