Monday, July 30, 2012

How can I save record using jQuery in ASP.Net?

Problem: How can I save record using jQuery in ASP.Net? Please provide me source code for save record using jquery in asp.net.

Solution:

You need to create a web service and add a web method for saving record and call the method from jquery.
Look at the following example:
You need to add a method in your web service which is marked with WebMethod attribute. Similar to following code snipppet:


[WebMethod]
        public bool MarkProjectComplete(long projectID)
        {
            bool result = false;
            EnumStatus status = ProjectFacade.MarkProjectComplete(projectID);
            if (status == EnumStatus.Successfull)
            {
                result = true;
                if (projectID > 0)
                {
                    Project project = null;
                    project = ProjectFacade.GetProjectByID(projectID);
                }
            }
            return result;
        }
 

Then call your method from your jquery function. Look at following code snippet:

function MarkProjectComplete(projectID) {
            if (confirm('Are you sure you would like to mark this project as ‘Mark Project Complete’?')) {
                $.ajax({
                    type: "POST",
                    url: "<%= ApplicationPath %>/WebServices/YourWebService.asmx/MarkProjectComplete",
                    data: "{'projectID' : '" + projectID + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                        if (msg.d == true) {
                            alert("Mark Project Complete Successfully");
                            
                        }
                    },
                    error: function () {
                    }
                });
            }
        }

Hope it will be helpful. Thanks.


1 comment: