"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Wednesday, August 11, 2010

Checking username avillabilty without page refresh or Connecting Javascript with web service

Hi friends,
2day I'm connecting javascript with Webservice Without help of any external js Files.and
i'm show username checking as an example.
Demo

















Code
 on Webservice
    <script runat="server">
        [System.Web.Services.WebMethod, System.Web.Script.Services.ScriptMethod]
        public static string Getadata(string Data)
        {
            System.Threading.Thread.Sleep(1000);// i'm making 1 second sleep here
            string Scriptt ="";
            if (Data == "arun")
                Scriptt = "Not Avilable";
            else
                Scriptt = "Avilable";
           
                return Scriptt;
        }
    </script>

on javascript

    <script language="javascript" type="text/javascript">
        function keyPress(id) {
           // debugger;
            var tb = document.getElementById(id);
            if (tb.value.length > 2) {
                document.getElementById('im').style.display = 'Block'; // Display Image
                document.getElementById('label1').innerHTML = "";
                PageMethods.Getadata(tb.value, myFunction);
               }
            return false;
        }
        function myFunction(msg) {
           // debugger;
            // alert(msg);
            hideimage();
            document.getElementById('label1').innerHTML = msg;
        }
        function hideimage() {
            document.getElementById('im').style.display = 'none'; 
// DOM3 (IE5, NS6) Hide Image
        }
    </script>

on Body
<body onload="hideimage();">
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager" runat="server"
EnablePageMethods="true" />
        asp.net:<asp:TextBox ID="TextBox1" onkeyup="keyPress('TextBox1');"
runat="server"></asp:TextBox><img id="im" src="Image/ajax-loader.gif" alt="" />
        <label id="label1" ></label>
       
        <asp:GridView ID="GridView1" runat="server">
        <Columns>
        <asp:TemplateField>
        <ItemTemplate>
        <asp:Button ID="but" runat="server" Text="data" onclick="but_Click" />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
       
    </div>
    </form>
</body>
 
Have a nice day... 'N happy Coding :)

2 comments:

Mourya said...

Hi Arun , though the concept seems to be good, but it would more easy to understand if the code contains proper comments where ever necessary and also please explain about the code and implementation

SImplyAsp said...

Ya, Actually the Code having On grid view that not in use actually i copy the code from my project i'm sorry, :).

---------------------------------------------

On the textbox(TextBox1) onkeyup event i'm calling a javascript that carrying the textbox ID. on javascript i'm checking the count of
character's if the character count greater than 2 it will display an image and also i'm making label1 innertext null. PageMethods.Getadata(tb.value, myFunction); PageMethods is used to Calling a Page Method In Javascript. Getadata is my fuction on webservice on that i'm passing two parameter 1.textbox content (tb.value) 2.A function that catch the value when i return from webservice (myFunction). ##On the webservice## I'm making a sleep for 1 sec to show the image and i'm checking it valilable or not if and return result.the result will catch on myFunction(msg), Msg will contain the result. on their i hidding my image and showin g my result on label1 as inner html. Game Ends here :) . if u need anything more clear pls replay. Hapy to Help.