我在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功能。我该怎么办?提前感谢您的见解。
干杯, 尼沙
答案 0 :(得分:1)
触发JavaScript Code + Server代码的另一种方法是在我们使用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
希望这有帮助,让我知道!
答案 1 :(得分:0)
所以在@lucidgold的帮助下,终于现在正在工作!解决方案是在按钮服务器端调用javascript函数。经过调整后,这里有完整的解决方案:
删除按钮上的useSubmitBehaviour属性:
<asp:Button ID="TestButton" Text="Test Me" OnClick="TestButton_Click" runat="server"/>
在C#中运行RegisterStartupScript,如下所示:
ClientScript.RegisterStartupScript(GetType(), "script", "<script type ='text/javascript'> getListOfPOI(); </script>");