Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hello everyone,
 
I have one page in which i check availability of users , i have made this through XML Http request in asp.net, the problem is that the status object always returns 0
 
my code is :
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
 
<!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">
 
        var xmlhttp;
 
        function CheckUserNameAvailability() {
 
            xmlhttp = null;
 

 

//            if (username = "" || username.length < 4) {

//                document.getElementById("<%=lblMsg.ClientID%>").innerHTML = "";

//                return false;

//            }

 

            if (window.XMLHttpRequest) {
 
                // code for all new browsers

                xmlhttp = new XMLHttpRequest();
 
            }
 
            else if (window.ActiveXObject) {
 
                // code for IE5 and IE6xmlhttp=

                new ActiveXObject("Microsoft.XMLHTTP");
 
            }
 
            if (xmlhttp != null) {
                alert("AjaxPageCalled");
                var username = document.getElementById("txtUserName").value;
                alert(username);
                xmlhttp.onreadystatechange = state_Change;
 

                xmlhttp.open("GET", "AjaxEnginePage.aspx?UserName=" + username, true);
 
                xmlhttp.send(null);
 
            }
 
            else {
 
                alert("Oops..Your browser does not support XMLHTTP.");
 
            }
 
        }
 

 
        function state_Change() {
 
           // alert("state_chage");
            //alert(xmlhttp.readyState);
            //alert(xmlhttp.status);
            if (xmlhttp.readyState == 4) {
 
                // 4 = “loaded”

                if (xmlhttp.status == 200) {
 
                    //request was successful

                    //check if username is available and message

                    var lblMesg = document.getElementById("<%=lblMsg.ClientID%>");
 
                    if (xmlhttp.responseText == "True") {
 
                        lblMesg.style.color = "Green";
 
                        lblMesg.innerHTML = "Username available.";
 
                    }
 
                    else {
 
                        lblMesg.innerHTML = "Username already in use.";
 
                        lblMesg.style.color = "Red";
 
                    }
 
                }
 
                else {
 
                    alert("Oops..username availability could not be checked.");
 
                }
 
            }
 
        }
 
    </script>
 

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table cellpadding="0" cellspacing="2">
 
    <tr>
 
        <td>User Name:</td>
 
        <td>
 
            <asp:TextBox ID="txtUserName" runat="server" ></asp:TextBox>
 
            <asp:Label ID="lblMsg" runat="server"></asp:Label>
 
        </td>
 
    </tr>
 
    <tr>
 
        <td>Email:</td>
 
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
 
    </tr>
 
    <tr>
 
        <td></td>
 
        <td><asp:Button ID="btnRegister" runat="server" Text="Register" OnClientClick ="CheckUserNameAvailability();" /></td>
 
    </tr>
 
   </table>
 
    </div>
    </form>
</body>
</html>
 
Ajax Engine Page.cs:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
 
public partial class AjaxEnginePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserNameAvailability();
       
    }
    public void CheckUserNameAvailability()
    {
        if (Request["UserName"] != null)
        {
            string username = Request["UserName"].ToString ();
            string result = "False";
            System.Web.Security.MembershipUser user = Membership.GetUser(username);
            if (user != null)
            {
                result = "False";
            }
            else
            {
                result = "True";
            }
 
            Response.Clear();
            Response.Write(result);
            Response.End();
        }
    }
 
}
 
Please help ..
 
Regards,
Krunal
Posted 29-Sep-12 4:32am
Comments
Sandeep Mewara at 29-Sep-12 10:02am
   
Is the request hitting your defined page?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Move your jscript above Head section
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 230
1 PIEBALDconsult 150
2 DamithSL 125
3 BillWoodruff 108
4 Garth J Lancaster 90
0 OriginalGriff 5,790
1 DamithSL 4,601
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,195


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100