asp. net+jquery ajax no refresh login method

  • 2020-06-12 08:53:58
  • OfStack

Because the work needs to study the ajax of 1 js, the results are as follows.
Mainly consists of three parts:
Note return false in $("#btn_login") in js. This prevents the server from rotating or it will refresh

$(document).ready(function () {
    $("#btn_login").click(function () {
        postlogin();
        return false;
    });
});

function postlogin() {
    if (checkUserName() && checkUserPwd()) {
        var username = $('#txt_loginname').val();
        var userpass = $('#txt_loginpass').val();
        $.post("../UserLogin.aspx", { UserName: username, UserPass: userpass }, function (result) {
            if (result == "1") {
                alert(" Login successful! ");
            } else if (result == "3") {
                alert(" The user name is incorrect !");
            } else if (result == "2") {
                alert(" Incorrect password !");
            } else {
                alert(" Login failed ! Please try again !" + result);
            }
        });
    }
}

function checkUserName() {
    if ($("#txt_loginname").val().length == 0) {
        alert(' The user name cannot be empty !');
        return false;
    } else {
        return true;
    }
}

function checkUserPwd() {
    if ($("#txt_loginpass").val().lenght == 0) {
        alert(' Incorrect password !');
        return false;
    } else {
        return true;
    }
}

2. Page section

<table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="32%" height="37" valign="middle"> User name: </td>
            <td width="68%" valign="middle">
                <input type="text" name="txt_loginname" id="txt_loginname" class="input_1"/>
            </td>
          </tr>
          <tr>
            <td height="37" valign="middle"> Password: </td>
            <td valign="middle">
            <input type="password" name="txt_loginpass" id="txt_loginpass" class="input_2"/>
            </td>
          </tr>
          <!--<tr>
            <td height="37" valign="middle"> Verification code: </td>
            <td valign="middle">
              <input type="text" name="textfield3" id="textfield3" class="input_3" style="float:left"/>
              <span style="float:left; margin-left:6px;"><img src="images/img_7.gif" /></span></td>
          </tr>-->
          <tr>
            <td colspan="2">
              <input type="image" name="btn_login" id="btn_login" src="images/img_4.gif" />
              <input type="image" name="input" src="images/img_5.gif" />
               <input type="image" name="input" src="images/img_6.gif" />
            </td>
          </tr>
        </table>

3. The background part is the page to process login information in js

protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request.Form["UserName"];
            string userpass = Request.Form["UserPass"];
            T_User user = UserManager.loginpassword(username, userpass);
            if (user != null)
            {
                Session["user"] = user;
                Response.Write("1");  // Login successful 
                Response.End();

            }
            else
            {
                if (UserManager.OnlyOne(username) >= 1)
                {
                    Response.Write("2");  // Incorrect password 
                    Response.End();
                }
                else
                {
                    Response.Write("3");  // The username does not exist 
                    Response.End();
                }
            }
        }

Related articles: