-2

Hi i wanted to know is it possible to call a codebehind function in my Javascript? The javascript is in the head of my asp.net webpage page.

I have tried many methods but none seem to have worked. Its the first time iv had to do this so an example would be a nice learning curve fix.

My codebehind function in 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;
    }
}

My 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>
3
  • 1
    And where do you want to call your serverside function in your javascript code? You can achieve this by using AJAX. You can create an asmx webservice for this. Commented Apr 28, 2014 at 8:55
  • I have looked at AJAX and it seems to be what i am looking for but i knew it could call jquery but was unsure about javascript (as examples i have seen do not use javascript) thanks Commented Apr 28, 2014 at 9:00
  • @user3535615 jQuery is Javascript Commented Apr 28, 2014 at 9:11

2 Answers 2

0

W3Schools have a complete tutorial (http://www.w3schools.com/ajax/default.asp) on Ajax. AJAX is asynchronous javascript and XML, so its pretty obvious javascript can make it happen. If you want just your text change event to fire asynchronously, then I would suggest you to use Update panel, that will save you a lot of trouble.

Sign up to request clarification or add additional context in comments.

Comments

0

you can use webservice as shown below..

code behind

 [WebMethod]
    public static string abc()
    {
        //function body
    }

In javascript..

 PageMethods.abc(onSucess2, onError2);

        function onSucess2(result) {

        }
        function onError2(result) {
            alert('Error!!!');
        }

You must place a scriptmanager in your page and set enabalepagemethods=true

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.