JavaScript:调用C#代码隐藏函数

开发人员

嗨,我想知道是否可以在Javascript中调用codebehind函数?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>
沙尚克(Shashank Chaturvedi)

W3Schools在Ajax上有完整的教程(http://www.w3schools.com/ajax/default.asp)。AJAX是异步javascript和XML,因此其非常明显的javascript可以实现这一目标。如果您只希望异步触发文本更改事件,那么建议您使用“更新”面板,这将为您省去很多麻烦。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章