65.9K
CodeProject is changing. Read more.
Home

Calling a C# method using jQuery on the client side

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.47/5 (11 votes)

Sep 8, 2011

CPOL
viewsIcon

31393

Create a web service name WebMethod.SVC in the CS file and add the method: [OperationContract] [WebInvoke(Method = "POST")] public void DeleteRec(string ID) { DelUser(Convert.ToInt32(ID)); }On client side, give the path and call the...

Create a web service name WebMethod.SVC in the CS file and add the method:
[OperationContract]
        [WebInvoke(Method = "POST")]
        public void DeleteRec(string ID)
        {
            DelUser(Convert.ToInt32(ID));
        }
On client side, give the path and call the method:
function DeleteMessage() {            
            var ID = $('[id$="hdnUserId"]').val();
            var url = '<%=ResolveUrl("~/WebMethods.svc/DeleteRec")%&  gt;';            
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: '{"ID":"' + ID + '"}',
                dataType: "json",
                success: function (response) {
                    document.location.href = '<%= ResolveUrl("~/Test1.aspx")%>';
                },
                error: function (xhr, ajaxOptions, thrownError) {
                }
            });
        }
This was the other way which I got.