AjaxPro的应用

自己用AjaxPro做了一个简单登陆,应用AjaxPro需要在配置文件中做修改

在<system.web></system.web>标签对中添加如下代码

<httpHandlers>
   <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
 </httpHandlers>

配置文件修改后即可应用。再看以下登陆操作

HTML:

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ajaxpro.aspx.cs" Inherits="Login_ajaxpro" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript">
        function Login_ButtonClick(){
            var username = document.getElementById("Text1").value;
            var password = document.getElementById("Text2").value;
            document.getElementById("loading").style.display="block";
            Login_ajaxpro.UserName(username,password,CallBackServer);
        }
        function CallBackServer(res){
            var success = res.value;
            document.getElementById("loading").style.display="none";
            if(success){
                alert("登陆成功");
            }
            else{
                alert("登陆失败,请检查您的用户名跟密码!");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
       <div>
            <input id="Text1" type="text" />
            <input id="Text2" type="text" />
            <input id="Button1" type="button" value="button" onclick="Login_ButtonClick();" />
            <div id="loading" style="display:none;">正在登陆......</div>
       </div>
    </form>
</body>
</html>

 

.CS

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;
using System.Data.SqlClient;

public partial class Login_ajaxpro : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Login_ajaxpro)); //注册AjaxPro
    }

    [AjaxPro.AjaxMethod]
    public bool UserName(string username, string password)
    {
        System.Threading.Thread.Sleep(3000);
        string strName = username.ToString();
        string strPWD = password.ToString();
        string strConn = ConfigurationSettings.AppSettings["connectinString"].ToString();
        SqlConnection conn = new SqlConnection(strConn);
        conn.Open();
        SqlCommand com = new SqlCommand();
        com.CommandText = "select * from users where username = '" + strName + "' and password = '" + strPWD + "'";
        com.Connection = conn;
        SqlDataReader da = com.ExecuteReader();
        if (da.Read())
        {
            conn.Close();
            return true;
        }
        else
        {
            conn.Close();
            return false;
        }
    }
}

posted @ 2008-11-03 21:53  WCJ  阅读(672)  评论(10编辑  收藏  举报