现在的位置: 首页 > 综合 > 正文

Ext+NET登录功能,实现与后台数据库的连接以及按回车提交功能

2012年11月10日 ⁄ 综合 ⁄ 共 3165字 ⁄ 字号 评论关闭

//Ext.JS

function LoginForm() {

    var loginPanel = new Ext.form.FormPanel({
        id: "loginPanel",
        labelPad: 0,
        labelWidth: 60,
        frame: true,
        labelAlign:"left",
        items: [
        { xtype: "field", id: "userName", fieldLabel: "用户名" },
        { xtype: "field", id: "password", fieldLabel: "密   码" }
        ]

    });

    var loginWindow;
    if (!loginWindow) {
        loginWindow = new Ext.Window({
            id: "loginWindow",
            title: "登录窗口",
            width: 230,//Window宽度
            height: 137,//Window高度
            resizable: false,/*是否可手动调整Window大小,默认为true*/
            closable: false,//关闭按钮,默认为true
            items: [loginPanel],
            buttons: [
            { xtype: "button", text: "确定", pressed: true, handler: validatorData }, /*pressed表示按钮是否按下,默认为false*/
            {xtype: "button", text: "取消", handler: function() { loginWindow.close(); } }]/*点击取消关闭Window,也可以调用loginWindow.hide()隐藏这个Window*/

        });
    }

    loginWindow.show();
    //按回车时调用确定事件
    var map = new Ext.KeyMap(loginWindow.getEl(), {
        key: 13,//回车的键盘key值
        fn: validatorData    //确定事件   
    });  
    //确定事件
    function validatorData() {
   
    var userName=Ext.getCmp("userName").getValue();
    var Password = Ext.getCmp("password").getValue();
    if (Ext.util.Format.trim(userName) == "" || Ext.util.Format.trim(Password)=="")
    {
        Ext.Msg.alert("警告","请正确输入数据,用户名和密码都不能够为空!");
        return;
    }
    //数据库连接及处理
    Ext.Ajax.request({
        url: "LoginValidator.aspx",//登录处理页面
        params: {ParamUserName: userName, ParamPassword: Password },//参数
        success: function(response, option) {
        var obj = Ext.util.JSON.decode(response.responseText); /*decode将json字符串转换成对象;(对应的是encode将对象转换成json字符串)*/
            if (obj.success == true) {
                Ext.Msg.alert("登录成功", "登录成功!");
            }
        },
        failure: function() {
            Ext.Msg.alert("登录失败", "登录失败!");
        }
    });
     
    }
}

Ext.onReady(LoginForm);

 

 

/*===========LoginValidator.aspx代码============*/

//要把其他的页面代码都去掉,只保留第一行,类似下面的代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ExtExmaple.Login.Login" %>

/*==========LoginValidator.aspx.cs代码========================*/

protected void Page_Load(object sender, EventArgs e)
    {

            string UserName = Request.QueryString["ParamUserName"];
            string Password = Request.QueryString["ParamPassword"];
            //连接数据库
            #region 连接数据库
            /*
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "连接字符串";
            if (conn.State == ConnectionState.Closed)
                conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = string.Format(" select UserID from 表 where UserName = '{0}' and Password = '{1}'", UserName, Password);
            object obj = cmd.ExecuteScalar();
            if (obj != null)
            {
                if (Convert.ToString(obj) != "")
                {
                    Response.Write("{success:true}");
                }
                else
                {
                    Response.Write("{success:false}");
                }
            }
            else
            {
                Response.Write("{success:false}");
            }
            */
            #endregion
            Response.Write("{success:true}");
       
    }

抱歉!评论已关闭.