【发布时间】:2014-06-13 17:38:49
【问题描述】:
您好,我想知道是否可以在我的 Javascript 中调用代码隐藏函数? javascript 在我的 asp.net 网页的头部。
我尝试了很多方法,但似乎都没有奏效。这是 iv 第一次必须这样做,所以一个例子将是一个很好的学习曲线修复。
我在 C# 中的代码隐藏函数
protected void GetAverageRent_TextChanged(object sender, EventArgs e)
{
string Postcode = _postCodeInput.Value.ToString();
var webGet = new HtmlWeb();
var doc = webGet.Load("http://www.webadress.com/" + Postcode);
HtmlNode AvgPrice = doc.DocumentNode.SelectSingleNode("//div[@class='split2r right']//strong[@class='price big']");
if (AvgPrice != null)
{
AverageRentLbl.Text = AvgPrice.InnerHtml.ToString();
}
else
{
AverageRentLbl.Text = "Invalid Postcode!";
AverageRentLbl.ForeColor = Color.Red;
AverageRentLbl.Font.Bold = true;
}
}
我的 Javascript
<script>
function codeAddress() {
document.getElementById('map-canvas').setAttribute("style", "height:200px");
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 16,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
//var address = document.getElementById('_mainContentHolder__postCodeValue').value;
var address = document.getElementById('ContentPlaceHolder1__postCodeInput').value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
document.getElementById('ContentPlaceHolder1__latValue').value = results[0].geometry.location.lat();
document.getElementById('ContentPlaceHolder1__longValue').value = results[0].geometry.location.lng();
} else {
alert('Sorry the postcode you entered in invalid. Please try again: ' + status);
document.getElementById('ContentPlaceHolder1__postCodeInput').value = "";
}
});</Script>
【问题讨论】:
-
你想在你的javascript代码中调用你的服务器端函数吗?您可以通过使用 AJAX 来实现这一点。您可以为此创建一个 asmx 网络服务。
-
我看过 AJAX,它似乎是我正在寻找的,但我知道它可以调用 jquery,但不确定 javascript(因为我看到的示例不使用 javascript)谢谢
-
@user3535615 jQuery 是 Javascript
标签: c# javascript asp.net