Wednesday 13 June 2012

Call Ajax Method from JavaScript



Hi   Friends..
Here is Some Simple Coding About HOW TO CALL SERVER SIDE METHOD FROM JAVASCRIPT

1 .  Write HTML For Any Control from which You want to Call Javascript as Follow, Here We
      Have A button on Its Click Event JS Function will Called.

<input type="button" value="sd" onclick="me();" />

2.   Now Write Java Script Function that Call Server Side Method
<script type="text/javascript">
        function me() {
        var result = _00Test.Hello();
        alert(result.value);
        }
</script>
     Here  “_OOTest” is the class Of Page  and “Hello()” is function Name, result will
     Occupy value that returns from called function.

3.   Now Create Ajax Method in Code Behind Like :

Imports Ajax

Partial Class _00Test
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
                            Handles Me.Load
           Ajax.Utility.RegisterTypeForAjax(GetType(_00Test))
    End Sub
    ''' <summary>
    '''  Function That Called from JavaScript
    ''' </summary>
    <Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)> _
    Public Function Hello() As String
        Return "Ajay Patel"
    End Function
End Class

     In this we have Used Ajax to call Server Side Method without Postback Of Page
     function “Hello()”  Contains attribute like “<Ajax.AjaxMethod(…)>” which Point
     that this method is Ajax Method that we can Call from JavaScript.

    
     This Is Simple Example about How to call Server side method using  JavaScript



No comments:

Post a Comment