Click here to Skip to main content
15,896,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 :

XML
<%@ 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
Comments
Sandeep Mewara 29-Sep-12 10:02am    
Is the request hitting your defined page?
[no name] 1-Oct-12 1:06am    
yes ... i have inserted a breakpoint on the page ...and it stops at the breakpoint..but dont know why the status always shows 0

1 solution

Move your jscript above Head section
 
Share this answer
 
Comments
[no name] 1-Oct-12 1:20am    
Hi,

it didn't solved my problem, i am getting same output again... Status is 0 still..

please help..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900