100

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);
        }
    }
}
查看全部
baidu_32111313
Lee丶Co
2017/06/15 16:39
  • 浏览器
  • asp.net
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

3个回复