Ajax 用户登录

Ajax 用户登录

  我们在编程过程中,处理用户登录会刷新整个页面,如果数据量小还好,如果加载大量数据,延时很严重,给用户感觉很不好,并且影响了网站性能。下面介绍一下 使用AjaxPro实现静态提交:
1。首先在web.config里添加
  <httpHandlers>
   <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro"/>
  </httpHandlers>

2。在App_code中新建类 AjaxLogin

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Resources;
using System.Collections;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Configuration;

using ******.Web;
using ******.Model;
using ******.DBUtility;
using ******.Log;
using AjaxPro;
namespace ******.SQLServerDAL
{
    /** <summary>
    /// Ajax 的摘要说明。
    /// </summary>
    public class AjaxLogin
    {    
        string returnstr="";
        
        public AjaxLogin()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        [AjaxPro.AjaxMethod]
        public string Login(string username,string password)
        {

            //return "";
            
            //check username and password
            //判断输入值是否有效
            Customer customer = new Customer();

            int UserID = customer.Login(username, password);
            string UserID = UserID.ToString();

            if (UserID != -1)
            {
                //Session.Clear();

                System.Web.HttpContext.Current.Session.Add("UserID", UserID);

                CustomerDetails cd = customer.GetDetails(UserID);
                
                System.Web.HttpContext.Current.Session.Add("UserName", cd.UserName);
                System.Web.HttpContext.Current.Session.Add("FirstName", cd.RealName_first);
                System.Web.HttpContext.Current.Session.Add("SecondName", cd.RealName_second);
                System.Web.HttpContext.Current.Session.Add("Email", cd.Email);
                System.Web.HttpContext.Current.Session.Add("Telephone", cd.Telephone);
                System.Web.HttpContext.Current.Session.Add("Country", cd.Country);
                System.Web.HttpContext.Current.Session.Add("State", cd.State);
                System.Web.HttpContext.Current.Session.Add("City", cd.City);
                System.Web.HttpContext.Current.Session.Add("Address", cd.Address);
                System.Web.HttpContext.Current.Session.Add("PostNum", cd.PostNum);
                System.Web.HttpContext.Current.Session.Add("IsBlog", cd.IsBlog);

                System.Web.HttpContext.Current.Session.Add("SchoolID", cd.SchoolID);
                System.Web.HttpContext.Current.Session.Add("UserSign", cd.UserSign);

             
                returnstr = "<TABLE cellSpacing=0 cellPadding=0 border=0 align=center><TR><td width=12></td><TD width=560 align=left height=27 valign=top><img src=/icon_memedit.gif align=top border=0> " + username + "<a href=/SignOut.aspx class=black-words>退出</a></td></TR></TABLE>";
               

              
                if (System.Web.Security.FormsAuthentication.GetRedirectUrl(UserID, false).ToLower().EndsWith("default.aspx"))
                {
                    // creates authentication ticket for user then display confirmation
                    System.Web.Security.FormsAuthentication.SetAuthCookie(UserID, false);
                }
                else
                {
                    // user came here by accessing a secure page, continue
                    // on to the page they were trying to access
                    System.Web.Security.FormsAuthentication.RedirectFromLoginPage(UserID, false);
                }

            }
            else
            {
                returnstr = "<TABLE cellSpacing=0 cellPadding=0 border=0 width=0><TR><td height=36>登录出错</td><TD align=left valign=Middle>  <img id='HiddenMessage' οnclick='Hidden();' src=/icon1.gif></td></TR></TABLE>";
            }
            return returnstr;
        }
    }
}

3.调用页Code



<%@ Control Language="c#" Inherits="******.Web.Controls.Control_first" CodeFile="Control_first.ascx.cs"
    CodeFileBaseClass="******.Web.ControlBase" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="******.SQLServerDAL" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<div id="Login">
    <asp:Panel ID="lin" runat="server">
        <table cellspacing="0" cellpadding="0" width="100%" border="0">
            <!--DWLayoutTable-->
            <tr>
                <td width="161" height="30">
                     </td>
                <td width="31" valign="middle" align="right">
                    <img height="13" src="/icon_memedit.gif" width="13"></td>
                <td class="txt3" valign="middle" align="center" width="79">
                    <%=_Lang["ControlTop_UserID"]%>
                </td>
                <td valign="middle" width="91">
                    <!--UserName-->
                    <input type="text" id="txtUsername" size="12" class="inputnormalleft">
                    <!--<asp:textbox id="txtUsername0" CssClass="inputnormalleft" Width="95" runat="server"></asp:textbox></TD>-->
                    <td valign="middle" width="66">
                        <div align="center">
                            <%=_Lang["ControlTop_Password"]%>
                        </div>
                        <td valign="middle" width="84">
                            <!--Password-->
                            <input class="inputnormalleft" id="Text1" type="password" maxlength="20" name="Text1"
                                size="12">
                            <asp:TextBox ID="txtPassword" runat="server" Columns="15" EnableViewState="False"
                                TextMode="Password"></asp:TextBox>
                            <%
                                string miyao = ConfigurationSettings.AppSettings["encryptionkey"];
                            %>
                            <input id="md5pass" type="hidden" value="<%=miyao%>" name="md5pass">
                        </td>
                <td width="60" valign="center">
                    <div align="center">
                        <!--Login-->
                        <button id="button_submit" οnclick="Login();" class="button_h_h" οnmοuseοver="bs(this)"
                            οnmοuseοut="bs(this)">
                            登录
                        </button>
                    </div>
                </td>
                <td width="16">
                    <div align="center">
                        <img height="13" src="/icon_photo.gif" width="13"></div>
                </td>
                
                <td width="200" valign="top">
                    <!--message for login-->
                    <asp:Label ID="message" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </asp:Panel>
</div>
<!--login-->
<div id="Message" align="center" style="vertical-align: middle; font-weight: bold;
    font-size: 12pt; color: #ffcc33; font-family: Tahoma, Verdana, Arial">
</div>
<asp:Panel ID="lout" runat="server">
    <table cellspacing="0" cellpadding="0" border="0">
        <tr>
            <td valign="middle" width="275" height="28">
                 </td>
            <td align="left">
                <img height="13" src="/icon_memedit.gif" width="13">
                <asp:Label ID="Name" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
</asp:Panel>
<!-- 顶端结尾 -->

<script type="text/javascript">
    function Login()
    {    
        //检查用户名 密码
        
        var str;
        if(document.form1.txtUsername.value=="")
        {    
              alert('输入用户名');
              form1.txtUsername.focus();
              return false;
        }
        if(document.form1.Text1.value=="")
        {
              alert('输入密码');
              form1.Text1.focus();
              return false;
        }
        
        document.getElementById('Message').style.display = "inline";
        document.getElementById('Login').style.display = "none";
        document.getElementById('Message').innerHTML = '正在为您登录请等候' ;
        //str = document.form1.md5pass.value
        ******.SQLServerDAL.AjaxLogin.Login(document.form1.txtUsername.value,document.form1.md5pass.value,GetValue);

    }
    function GetValue(rel)
    {//要加上.value
        document.getElementById('Login').style.display = "none";
        document.getElementById('Message').style.display = "inline";
        document.getElementById('Message').innerHTML =rel.value;
    }
    //hidden the message
    function Hidden()
    {    
        document.getElementById('Login').style.display = "inline";
        document.getElementById('Message').style.display = "none";
        document.getElementById('Text1').value="";
    }

</script>


 

 4。后台脚本


protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxLogin));
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值