How to update data using Ajax and webservice ?



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"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

How to display custom error message using CSS and validation control ?



Hi

Recently i was doing some test using CSS.Then i got very nice css, which one we can use in our real time project.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomCSSTest.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head id="Head1" runat="server">
<title></title>
<style type="text/css">
.errors
{
border: solid 1px Blue;
background-color: #d0e6ff;
padding: 10px;
}

</style>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
runat="server" ErrorMessage="Enter Your Name Please!" CssClass="errors" ForeColor="Blue"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" Text="Button" />
</td>
<td>
</td>
</tr>
</table>
<div>

</div>
</form>
</body>
</html>

How to display Balloon Popup using ajax Toolkit ?


Hi

Using ajax control we can display very nice ballon popup like this

<asp:Panel ID="Panel2" runat="server">
This Balloon Popup uses the Cloud style.
</asp:Panel>
<ajaxToolkit:BalloonPopupExtender ID="PopupControlExtender2" runat="server" TargetControlID="MessageTextBox"
BalloonPopupControlID="Panel2" Position="BottomRight" BalloonStyle="Cloud" BalloonSize="Small"
UseShadow="false" DisplayOnClick="true" DisplayOnFocus="true" />

I hope it will help to create nice web base portal.