如果您有任何意见,请留下您宝贵的意见以便告知
操作环境:Visual Studio.Net2003
操作系统:window Xp SP2
就上次分析的简易Ajax框架一篇没能给你带来什么,再或者Ajax加减乘除运算也没能告诉你什么,只能告诉你,或者是对Ajax的更进一步理解,现在用那个框架实现无刷新的用户验证,希望能给你带来点什么
//客户端登录页面 Login.aspx ;代码如下
<
HTML
>
< HEAD >
< title > HelloWorld </ title >
< meta name = " GENERATOR " Content = " Microsoft Visual Studio .NET 7.1 " >
< meta name = " CODE_LANGUAGE " Content = " C# " >
< meta name = " vs_defaultClientScript " content = " JavaScript " >
< meta name = " vs_targetSchema " content = " http://schemas.microsoft.com/intellisense/ie5 " >
< script language = " jscript " src = " ../Javascript/CallBackObject.js " ></ script >
< script language = " jscript " >
function createRequest()
{
var name = escape(document.getElementById( " name " ).value);
var pass = escape(document.getElementById( " pwd " ).value);
var cbo = new CallBackObject();
cbo.OnComplete = Cbo_Complete;
cbo.onError = Cbo_Error;
cbo.DoCallBack( " Ajax.aspx?name= " + name + " &password= " + pass);
}
function Cbo_Complete(responseText, responseXML)
{
alert(responseText);
}
function Cbo_Error(status, statusText, responseText)
{
alert(responseText);
}
</ script >
</ HEAD >
< body >
< form id = " Form1 " method = " post " runat = " server " >
< asp:Label id = " lblUsername " runat = " server " Width = " 70 " > UserName: </ asp:Label >< INPUT id = " name " style = " WIDTH: 192px; HEIGHT: 22px " type = " text " size = " 26 " >
< br >
< asp:Label id = " lblPassword " runat = " server " Width = " 70px " > Password: </ asp:Label >< INPUT id = " pwd " style = " WIDTH: 192px; HEIGHT: 22px " type = " text " size = " 26 " >
< br >
< input type = " button " value = " Login! " onclick = " createRequest() " style = " WIDTH: 128px; HEIGHT: 24px " >
</ form >
</ body >
</ HTML >
< HEAD >
< title > HelloWorld </ title >
< meta name = " GENERATOR " Content = " Microsoft Visual Studio .NET 7.1 " >
< meta name = " CODE_LANGUAGE " Content = " C# " >
< meta name = " vs_defaultClientScript " content = " JavaScript " >
< meta name = " vs_targetSchema " content = " http://schemas.microsoft.com/intellisense/ie5 " >
< script language = " jscript " src = " ../Javascript/CallBackObject.js " ></ script >
< script language = " jscript " >
function createRequest()
{
var name = escape(document.getElementById( " name " ).value);
var pass = escape(document.getElementById( " pwd " ).value);
var cbo = new CallBackObject();
cbo.OnComplete = Cbo_Complete;
cbo.onError = Cbo_Error;
cbo.DoCallBack( " Ajax.aspx?name= " + name + " &password= " + pass);
}
function Cbo_Complete(responseText, responseXML)
{
alert(responseText);
}
function Cbo_Error(status, statusText, responseText)
{
alert(responseText);
}
</ script >
</ HEAD >
< body >
< form id = " Form1 " method = " post " runat = " server " >
< asp:Label id = " lblUsername " runat = " server " Width = " 70 " > UserName: </ asp:Label >< INPUT id = " name " style = " WIDTH: 192px; HEIGHT: 22px " type = " text " size = " 26 " >
< br >
< asp:Label id = " lblPassword " runat = " server " Width = " 70px " > Password: </ asp:Label >< INPUT id = " pwd " style = " WIDTH: 192px; HEIGHT: 22px " type = " text " size = " 26 " >
< br >
< input type = " button " value = " Login! " onclick = " createRequest() " style = " WIDTH: 128px; HEIGHT: 24px " >
</ form >
</ body >
</ HTML >
代码解释:<script language="jscript" src="../Javascript/CallBackObject.js"></script> CallBackObject.js 就是前面提到的简易Ajax框架,再这就不提供了,详细请看另篇 简易Ajax框架解释【原创】
Login.aspx.cs 页面不需要什么,主要在客户端提交
服务器处理页面:AJAX.asp.cs
using
System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace AjaxFrmClass
{
/// <summary>
/// AjaxTest 的摘要说明。
/// </summary>
public class AjaxTest : System.Web.UI.Page
{
private void Page_Load( object sender, System.EventArgs e)
{
// show();
#region 用户登陆
switch (LoginVerify())
{
case " 1 " :
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write( " 密码不正确 " );
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
break ;
case " 2 " :
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write( " 用户名不存在 " );
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
break ;
default :
break ;
}
#endregion
}
#region 输入地用户名进行判断
/// <summary>
/// 对用户输入的用户名和密码进行有效性验证
/// </summary>
/// <returns></returns>
public string LoginVerify()
{
try
{
string strName = HttpContext.Current.Request.QueryString[ " name " ];
string strPwd = HttpContext.Current.Request.QueryString[ " password " ];
SqlParameter MyParameter = new SqlParameter();
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection( " uid=sa;pwd=1234;database=pubs " );
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = " SP_User_Login " ;
cmd.Connection = con;
MyParameter = cmd.Parameters.Add( " @RETURN_VALUE " ,SqlDbType.Int);
MyParameter.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add( " @name " ,strName);
cmd.Parameters.Add( " @pwd " , strPwd);
con.Open();
cmd.ExecuteNonQuery();
return cmd.Parameters[ " @RETURN_VALUE " ].Value.ToString();
}
catch (Exception errExce)
{
return errExce.Message.ToString();
}
finally
{
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this .Load += new System.EventHandler( this .Page_Load);
}
#endregion
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace AjaxFrmClass
{
/// <summary>
/// AjaxTest 的摘要说明。
/// </summary>
public class AjaxTest : System.Web.UI.Page
{
private void Page_Load( object sender, System.EventArgs e)
{
// show();
#region 用户登陆
switch (LoginVerify())
{
case " 1 " :
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write( " 密码不正确 " );
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
break ;
case " 2 " :
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write( " 用户名不存在 " );
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
break ;
default :
break ;
}
#endregion
}
#region 输入地用户名进行判断
/// <summary>
/// 对用户输入的用户名和密码进行有效性验证
/// </summary>
/// <returns></returns>
public string LoginVerify()
{
try
{
string strName = HttpContext.Current.Request.QueryString[ " name " ];
string strPwd = HttpContext.Current.Request.QueryString[ " password " ];
SqlParameter MyParameter = new SqlParameter();
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection( " uid=sa;pwd=1234;database=pubs " );
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = " SP_User_Login " ;
cmd.Connection = con;
MyParameter = cmd.Parameters.Add( " @RETURN_VALUE " ,SqlDbType.Int);
MyParameter.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add( " @name " ,strName);
cmd.Parameters.Add( " @pwd " , strPwd);
con.Open();
cmd.ExecuteNonQuery();
return cmd.Parameters[ " @RETURN_VALUE " ].Value.ToString();
}
catch (Exception errExce)
{
return errExce.Message.ToString();
}
finally
{
}
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base .OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this .Load += new System.EventHandler( this .Page_Load);
}
#endregion
}
}
Ajax.aspx 不需要做处理,此时只需要在服务器端处理即可
完成以上2个页面的操作,基本上算是成功了,就差一个登录的存储过程了
CREATE PROC SP_User_Login
(
@name VARCHAR(15),
@pwd VARCHAR(15)
)
AS
DECLARE @temp_Name VARCHAR(15)
BEGIN
SELECT @temp_Name=UserName
FROM t_UserLogin
WHERE UserName = @name
AND
Password = @pwd
IF @temp_Name IS NOT NULL
RETURN 0 --登录成功
IF EXISTS (SELECT UserName
FROM t_UserLogin
WHERE UserName = @name)
RETURN 1 --密码不正确
ELSE
RETURN 2 --用户名不存在
END
GO
(
@name VARCHAR(15),
@pwd VARCHAR(15)
)
AS
DECLARE @temp_Name VARCHAR(15)
BEGIN
SELECT @temp_Name=UserName
FROM t_UserLogin
WHERE UserName = @name
AND
Password = @pwd
IF @temp_Name IS NOT NULL
RETURN 0 --登录成功
IF EXISTS (SELECT UserName
FROM t_UserLogin
WHERE UserName = @name)
RETURN 1 --密码不正确
ELSE
RETURN 2 --用户名不存在
END
GO
存储过程写完了,Ctrl + F5即可看见效果