欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Asp.Net中Ajax实现登陆判断

程序员文章站 2022-06-11 16:12:37
...
Default.aspx:

<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
    var xmlhttp;
    function Login()
    {
    var userName=document.getElementById("TextBox1").value;
    var userPwd=document.getElementById("TextBox2").value;
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET","Login.aspx?UserName="+userName+"&UserPwd="+userPwd,true);
    xmlhttp.onreadystatechange=stateChange;
    xmlhttp.send(null);
    return false;
    }
    
    function stateChange()
    {
        if(xmlhttp.readystate==4&&xmlhttp.status==200)
        {
            document.getElementById("Label1").innerHTML=xmlhttp.responseText;
        }
    }
    </script>
    
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="100px"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server" Width="100px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="登陆" />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>
    </form>
</body>

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Button1.Attributes.Add("OnClick","return Login()");
    }

 


Login.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["UserName"] != null && Request.QueryString["UserName"].Length != 0 && Request.QueryString["UserPwd"] != null && Request.QueryString["UserPwd"].Length != 0)
        {
            if (Request.QueryString["UserName"] == "wu1987116" && Request.QueryString["UserPwd"] == "wu1987116")
            {
                Response.Write("登陆成功!");
            }
            else
            {
                Response.Write("用户名或密码错误!");
            }
        }
    }