Wednesday, 28 March 2012

check available in asp.net,jquery,json and webservice

design the page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default14.aspx.cs" Inherits="Default14" %>

<!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 language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" ></script>
    <script language="javascript">
        $(function () {
            $("#tb").blur(function () {
                $("#im").css("display", "block");
                $("#im1").css("display", "none");
                $("#im2").css("display", "none");
                $("#dv").text("");
                $.ajax(
                {
                    type: "POST",
                    url: "WebService.asmx/x",
                    data: "{'d':'" + $("#tb").val() + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        if (data.d == "0") {
                            $("#im").css("display", "none");
                            $("#im1").css("display", "block");
                            $("#dv").text('Available');
                            $("#dv").css("color", "Green");

                        }
                        else {
                            $("#im").css("display", "none");
                            $("#im2").css("display", "block");
                            $("#dv").text('Notavailable');
                            $("#dv").css("color", "Red");
                        }
                    },
                    error: function () {
                        alert('error');
                    }
                });
            });
        });

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="sm" runat="server"></asp:ScriptManager>
    <div>
    <asp:UpdatePanel ID="up" runat="server">
    <ContentTemplate>
     <center>
    <table>
    <tr>
    <td>
    <asp:TextBox ID="tb" runat="server"></asp:TextBox>
    </td>
    <td>
    <asp:Image ID="im" style="display:none; vertical-align:middle" runat="server" Height="16" Width="16" ImageUrl="~/image/progressbar.gif" />
    </td>
    <td>
    <asp:Image ID="im2" style="display:none" runat="server" Height="16" Width="16" ImageUrl="~/image/DeleteRed.png" />
    </td>
    <td>
    <asp:Image ID="im1" style="display:none" runat="server" Height="16" Width="16" ImageUrl="~/image/bright-green-tick-md.png" />
    </td>
    <td>
    <div id="dv"></div>
    </td>
    </tr>
    </table>
    </center>
    </ContentTemplate>
    </asp:UpdatePanel>
  
    </div>
    </form>
</body>
</html>
Add a web service on ur project code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Threading;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    DataClassesDataContext o = new DataClassesDataContext();
    string s;
    [WebMethod]
    public string x(string d)
    {
        var m = from y in o.EMPs where y.NAME==d select y;
        if (m.ToList().Count > 0)
            s = "1";
        else
            s = "0";
        Thread.Sleep(5000);
        return s;
    }
   
}
then page looks like




No comments:

Post a Comment