Lee丶Co 2017-06-15 16:39 采纳率: 100%
浏览 1542
已采纳

ASP.Net button第一次点击js中的回调函数不执行,第二次点击才执行

前台:

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CheIndexOnce.aspx.cs" Inherits="Monkey.Web.admin.CheIndexOnce" %>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>管理员登录</title>
    <link href="skin/login/Site.css" rel="stylesheet" />
    <script type="text/javascript" src="../scripts/jquery/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //检测IE
            if ('undefined' == typeof (document.body.style.maxHeight)) {
                window.location.href = 'ie6update.html';
            }
        });

        function jsFunction() {
            PageMethods.Encrypt(document.getElementById("txtUserName").value, jiami);
            PageMethods.Encrypt(document.getElementById("txtPassword").value, jiami2);
            if (document.getElementById("posx").value != "") {
                document.getElementById("txtPassword").value = "";
                return true;
            }
            return false;
        }



        function jiami(val)       //回传方法用val接受后台代码的执行结果
        {
            document.getElementById("posx").value = val;
        }
        function jiami2(val)       //回传方法用val接受后台代码的执行结果
        {
            document.getElementById("posx2").value = val;
        }
    </script>
</head>

<body class="login_body">
    <div class="login">
        <div class="content">
            <div class="logo">
                <div class="logo_content">
                    <img src="skin/login/180.png" />
                    <div class="right_logo">
                        <p></p>
                    </div>
                </div>
            </div>
            <div class="footer_content">
                <hr />

                <form id="form1" runat="server" class="form-login">
                     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"> </asp:ScriptManager>
                    <ul>
                        <li>
                        <li>
                            <input id="txtUserName" type="text" class="loginipt user login-input " placeholder="用户名" title="用户名" /></li>
                        <li>
                            <input id="txtPassword" type="password" class="loginipt password login-input" autocomplete="off" placeholder="密码" title="密码" /></li>
                        <li>
                            <label id="msgtip" runat="server" class="logintext"></label>
                        </li>
                            <asp:Button ID="btnSubmit" runat="server" Text="登录"  CssClass="loginbtn login-btn" OnClick="btnSubmit_Click" /> 
                         </ul>
                    <asp:HiddenField ID="posx" runat="server" />
                    <asp:HiddenField ID="posx2" runat="server" />
                </form>

            </div>
        </div>
        <div class="footer">
            <p></p>
        </div>
    </div>
</body>
</html>

后台:

 using System;
using System.Web.UI;
using Monkey.Common;
using System.Security.Cryptography;
using System.Configuration;
using System.Web;
using System.Text;
using System.Web.Services;


namespace Monkey.Web.admin
{
    public partial class CheIndexOnce : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //txtUserName.Text = Utils.GetCookie(DTKeys.COOKIE_URL_ADMIN_NAME);
                btnSubmit.Attributes.Add("OnClick", "return jsFunction()");  
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string key = "ae125efkk4454eeff444ferfkny6oxi8";
            string userName = "";
            string userPwd = "";
            try
            {
                userName = Request.Params["posx"];
                userPwd = Request.Params["posx2"];
                userName = Decrypt(userName, key);
                userPwd = Decrypt(userPwd, key);
            }
            catch {
                return;
            }

            if (userName.Equals("") || userPwd.Equals(""))
            {
                msgtip.InnerHtml = "请输入用户名或密码";
                return;
            }

            if (Session[DTKeys.COOKIE_URL_ADMIN_LOGIN_SUN] == null)
            {
                Session[DTKeys.COOKIE_URL_ADMIN_LOGIN_SUN] = 1;
            }
            else
            {
                Session[DTKeys.COOKIE_URL_ADMIN_LOGIN_SUN] = Convert.ToInt32(Session["AdminLoginSun"]) + 1;
            }
            //判断登录错误次数
            if (Session[DTKeys.COOKIE_URL_ADMIN_LOGIN_SUN] != null && Convert.ToInt32(Session[DTKeys.COOKIE_URL_ADMIN_LOGIN_SUN]) > 5)
            {
                msgtip.InnerHtml = "错误超过5次,关闭浏览器重新登录!";
                return;
            }

            BLL.manager bll = new BLL.manager();
            Model.manager model = bll.GetModel(userName, userPwd, true);
            if (model == null)
            {
                msgtip.InnerHtml = "用户名或密码有误,请重试!";
                return;
            }

            Session[DTKeys.SESSION_ADMIN_INFO] = model;
            Session.Timeout = 45;
            //写入登录日志
            Model.siteconfig siteConfig = new BLL.siteconfig().loadConfig();
            if (siteConfig.logstatus > 0)
            {
                new BLL.manager_log().Add(model.id, model.user_name, DTEnums.ActionEnum.Login.ToString(), "用户登录");
            }

            //写入Cookies
            Utils.WriteCookie(DTKeys.COOKIE_URL_ADMIN_NAME, model.user_name, 14400);

            //跳转
            Response.Redirect("index.aspx");
            return;
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="encryptStr">明文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        [WebMethod]
        public static string Encrypt(string encryptStr)
        {

                string key = "ae125efkk4454eeff444ferfkny6oxi8";
                byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="decryptStr">密文</param>
        /// <param name="key">密钥</param>
        /// <returns></returns>
        public static string Decrypt(string decryptStr, string key)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
            byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return UTF8Encoding.UTF8.GetString(resultArray);
        }
    }
}
  • 写回答

3条回答 默认 最新

  • Go 旅城通票 2017-06-16 02:48
    关注

    没用过微软的ScriptManager,自己看下是否有同步的配置,下面2个改同步请求,而不是异步
    PageMethods.Encrypt(document.getElementById("txtUserName").value, jiami);
    PageMethods.Encrypt(document.getElementById("txtPassword").value, jiami2);

    用死循环来挂起后续代码执行等异步操作结果返回行这个得看浏览器内核如何处理了,chrome应该是死循环也会执行异步回调,其他浏览器可能异步操作都挂起了,所以一直假死

    异步的话你得改变你的思路,不能在jsFunction调用Encrypt,而是改为txtUserName,txtPassword的blur事件中调用,原理的话caozhy已经说得很明白了

             $(function () {
                //检测IE
                if ('undefined' == typeof (document.body.style.maxHeight)) {
                    window.location.href = 'ie6update.html';
                }
                /////////
                document.getElementById("txtUserName").onblur = function () { PageMethods.Encrypt(document.getElementById("txtUserName").value, jiami); }
                document.getElementById("txtPassword").onblur = function () { PageMethods.Encrypt(document.getElementById("txtPassword").value, jiami2); }
            });
    
            function jsFunction() {//在blur事件中执行Encrypt请求
                ///PageMethods.Encrypt(document.getElementById("txtUserName").value, jiami);
                //PageMethods.Encrypt(document.getElementById("txtPassword").value, jiami2);
                if (document.getElementById("posx").value != "") {
                    document.getElementById("txtPassword").value = "";
                    return true;
                }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题