Hi
Recently in one of my friend project, there was requirement to update data using ajax and javascript to avoid the postback. Then we did like this.
Step1: Create a webservice and write the code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;namespace Ajax_Test
{
////// Summary description for WebService2
///[WebService(Namespace = “http://tempuri.org/”)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{[WebMethod]
public string UpdateEmpDetail(int Id,string EmpName)
{using (SqlConnection con = new SqlConnection(“Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True;User Instance=True”))
{
using (SqlCommand cmd = new SqlCommand(“Update tblEmp set EmpName=@EmpName where Id=@Id”, con))
{
cmd.Parameters.AddWithValue(“@Id”, Id);
cmd.Parameters.AddWithValue(“@EmpName”, EmpName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
string flag = “Record has been updated sucessfully”;
return flag;
}
}
}
}
}
Step2: Take one Ajax Script manager control and configure like this
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService2.asmx" />
</Services>
</asp:ScriptManager>
Step3: Write the Javacript code like this
<script type="text/javascript">
function updateEmpDB() {var name = document.getElementById("<%=txtName.ClientID %>").value;
Ajax_Test.WebService2.UpdateEmpDetail(Id, name, OnComplete, OnTimeOut, onerror);
}
function OnComplete(args) {
document.getElementById('Label1').innerHTML = args;
document.getElementById("<%=txtName.ClientID %>").value = "";
}
function OnTimeOut(args) {
alert("Service call timed out.");
}
function OnError(args)
{
alert("Error calling service method.");
}
</script>
Now the code working code will be like this
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Ajax_Test.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title><script type="text/javascript">
function updateEmpDB() {var name = document.getElementById("<%=txtName.ClientID %>").value;
Ajax_Test.WebService2.UpdateEmpDetail(Id, name, OnComplete, OnTimeOut, onerror);
}
function OnComplete(args) {
document.getElementById('Label1').innerHTML = args;
document.getElementById("<%=txtName.ClientID %>").value = "";
}
function OnTimeOut(args) {
alert("Service call timed out.");
}
function OnError(args)
{
alert("Error calling service method.");
}
</script></head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server" />
<br />
<input id="Button1" type="button" value="Update" onclick="return updateEmpDB();" />
<%–<asp:Button ID="btnClick" Text="Update" runat="server" OnClientClick="return updateEmpDB();" />–%>
<br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService2.asmx" />
</Services>
</asp:ScriptManager><asp:Label ID="Label1" runat="server" Font-Bold="true" ForeColor="Red" Text=""></asp:Label>
<br />
</div>
</form>
</body>
</html>