Tuesday 10 July 2012

Call server side function using JQuery/JSON

<script src="App_Script/jquery-1.4.3.min.js" type="text/javascript">
</script>

<script type="text/javascript">
        $(document).ready(function () {
            $("#<%= btnLogin.clientID %>").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "000Test.aspx/FillRelatedCategory1",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        async: true,
                        cache: false,
                        success: function (msg) {
                            $('#MSG').text(msg.d);
                        },
                        error: function (data) { alert("Error" + data.d); }
                    })
                    return false;
               
             });
        });
   
</script>

HTML
<div id="MSG">
 </div>
 <asp:Button runat="server" ID="btnLogin" />

.VB
<System.Web.Services.WebMethod()> _
    Public Shared Function FillRelatedCategory1() As String
        Return "Ajay"
    End Function
C#
[WebMethod]
public static string FillRelatedCategory1 ()
{
        return " Ajay ";
}

No comments:

Post a Comment