Hello,
I am working in c# asp.net
can i use AJAX for crud operations with c# asp.net code, not mvc.
Mine code is working, but without ajax performance is very slow some times..
Any sample code would be appericiated !!
Hello,
I am working in c# asp.net
can i use AJAX for crud operations with c# asp.net code, not mvc.
Mine code is working, but without ajax performance is very slow some times..
Any sample code would be appericiated !!
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Sam HobbsRank #107 · 20.5K reputationPosted Sep 19, 2025, 5:28 PM EDT
CRUD implies database. Databases are typically in a server, rarely in the client. AJAX is primarily a client-side technology.
Are you asking if it is possible to use AJAX in the client to make requests to a database in the server?
Prasad RaveendranRank #320 · 7.8K reputationPosted Sep 20, 2025, 12:58 AM EDT
I second Sam Hobbs comments. What exactly you are trying to achieve.
If you’re staying on Web Forms, use
[WebMethod](PageMethods) or.asmxservices.If you can, consider migrating to Web API (even in Web Forms) for modern REST-style CRUD with AJAX
Option 1 – Using
PageMethodswith Web FormsEmployee CRUD with AJAX // Insert Employee function addEmployee() { var name = $("#txtName").val(); var dept = $("#txtDept").val(); $.ajax({ type: "POST", url: "Employee.aspx/AddEmployee", data: JSON.stringify({ name: name, dept: dept }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert("Inserted Employee ID: " + response.d); }, error: function (err) { console.log(err); } }); } Name: Dept: Add Employeecode behind
using System; using System.Web.Services; namespace WebApp { public partial class Employee : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static int AddEmployee(string name, string dept) { // ?? Sample insert logic // Normally use ADO.NET/EF here Random rnd = new Random(); int newId = rnd.Next(1000, 9999); // Save to DB (pseudo-code) // using(SqlConnection con = new SqlConnection(...)) { ... } return newId; // return employee ID } } }Option 2 – Using a Web Service (
.asmx)You can create a lightweight Web Service and call it via AJAX.
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class EmployeeService : WebService { [WebMethod] public string InsertEmployee(string name, string dept) { // Insert into DB return "Inserted Successfully"; } [WebMethod] public string DeleteEmployee(int id) { // Delete from DB return "Deleted Successfully"; } }Then call it with AJAX:
$.ajax({ type: "POST", url: "EmployeeService.asmx/InsertEmployee", data: JSON.stringify({ name: "John", dept: "IT" }), contentType: "application/json; charset=utf-8", dataType: "json", success: function(response) { alert(response.d); } });Sandhiya PriyaRank #26 · 43.2K reputationPosted Oct 13, 2025, 2:59 AM EDT
Yes! You can absolutely use AJAX for CRUD operations in ASP.NET Web Forms (C#) without using MVC.
Using AJAX avoids full-page postbacks, which improves performance significantly. Here’s a clear explanation with a simple example.
1. Using AJAX in ASP.NET Web Forms
You have two main approaches:
a. Using
PageMethodsCreate static methods in your code-behind.
Call them from JavaScript via AJAX.
Code-Behind (C#)
using System; using System.Web.Services; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static string InsertData(string name, int age) { // Example: insert into database // Use ADO.NET here return $"Inserted {name} with age {age}"; } [WebMethod] public static string GetData() { // Return data as JSON return "[{\"Name\":\"John\",\"Age\":25},{\"Name\":\"Jane\",\"Age\":30}]"; } }ASPX Page (JavaScript)
function insertData() { var name = $("#txtName").val(); var age = $("#txtAge").val(); $.ajax({ type: "POST", url: "Default.aspx/InsertData", data: JSON.stringify({ name: name, age: parseInt(age) }), contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { alert(response.d); // response.d contains the returned string }, error: function (err) { console.log(err); } }); } function loadData() { $.ajax({ type: "POST", url: "Default.aspx/GetData", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (response) { console.log(response.d); // JSON string } }); } Insert LoadNotes:
Methods must be
[WebMethod]andstatic.Data is passed as JSON.
No full-page refresh → faster CRUD.
b. Using
UpdatePanel(Partial Postback)For simpler scenarios, you can wrap controls in an
UpdatePanel.Only the panel refreshes, not the whole page.
3. Benefits of Using AJAX
No full-page reload → faster.
Better user experience.
Can return JSON for easy rendering in JavaScript.