標籤:style blog http io ar java for 資料 sp
通過對Ext的學習,發現學習分三部曲:1.看官網的Demo,宏觀瞭解Ext能做什麼;2.看相關書籍,做理論指導;3.實現官網的Demo,體會Ext的真諦。
在完畢了第一、二部後,如今我們須要做的是實現第三步,我的目標效果是用Extjs畫頁面,用Asp.Net的一般處理常式做後台server實現登入功能。
宏觀上的指導有了,以下進行細分,我整理了實現過程,思路例如以下:
1.畫一個登入的Panel--loginPanel;
2.定義一個檢測是否登入成功的函數CheckLogin;
3.定義一個登入視窗--win,將loginPanel加入到登入視窗中;
4.在登入視窗中加入“確定”和“取消”,為“確定”button綁定登入驗證函式CheckLogin;
5.為登入視窗綁定一個最小化事件;
6.定義一個登入成功彈出新視窗函數loginSuccess
有了理論指導,以下進行代碼實現:
<!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> <title></title> <link href="css/ext-all.css" rel="stylesheet" type="text/css" /> <script src="js/ext-base.js" type="text/javascript"></script> <script src="js/ext-all.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> var loginPanel; Ext.onReady(function () { //1.畫一個登入的Panel loginPanel = new Ext.form.FormPanel({ labelWidth: 40, width: 230, height: 120, frame: true, bodyStyle: "padding:20", //給這個面板加入內間距 items: [ { xtype: "textfield", id: "userName", fieldLabel: "username", style: "margin:1 3 15 3", labelWidth: 10, emptyText: "請輸入username...", allowBlank: false, blankText: "username不可為空" }, { xtype: "textfield", id: "userPass", fieldLabel: "密 碼", style: "margin:1 3 15 3", labelWidth: 10, inputType: "password" } ] }); //2.定義一個檢測是否登入成功的函數CheckLogin function CheckLogin() { var userName = Ext.getCmp("userName").getValue(); //username var userPass = Ext.getCmp("userPass").getValue(); //密 碼 if (Ext.util.Format.trim(userName) == "" || Ext.util.Format.trim(userPass) == "") {//username和password不可為空 Ext.Msg.alert("提示", "username或password不可為空"); return; } Ext.Ajax.request({ url: "Extjs.ashx?userName=" + userName + "&passWord=" + userPass, //將username和password傳送到url method: "get", success: function (response) { var getData = response.responseText; //擷取伺服器資料 if (getData == "ok") { loginSuccess(); //登入成功了 } else { Ext.Msg.alert("警告", "登入失敗!"); } }, failure: function (response, options) { alert("失敗"); } }); } //3.定義一個登入視窗--win,將loginPanel加入到登入視窗中; var win = new Ext.Window({ title: "登入視窗", minimizable: true, //最小化 maximizable: true, //最大化 width: 240, height: 186, frame: true, items: [loginPanel], //4.在登入視窗中加入“確定”和“取消”,為“確定”按鈕綁定登入驗證函式CheckLogin buttons: [{ text: "確定", handler: CheckLogin }, { text: "取消"}] }); //5.為登入視窗綁定一個最小化事件 win.minimize = function () { Ext.MessageBox.confirm("提示", "是否要最小化", minwindow); function minwindow(btn) { if (btn == "yes") { Ext.Msg.alert("提示", "確定最小化麼", function () { alert("成功最小化"); }); } } } //6.定義一個登入成功彈出新視窗函數loginSuccess; function loginSuccess() { var winLoginSuccess = new Ext.Window({ title: "登入成功後的視窗", minimizable: true, //最小化 maximizable: true, //最大化 collapsible: true, width: 550, height: 550, frame: true, items: [{ xtype: "button", frame: true, text: "我是登入成功後視窗的一個按鈕"}] }); win.hide(); //登入視窗隱藏 winLoginSuccess.show(); //顯示登入成功後的視窗 } win.show(); //顯示視窗 }); </script></head><body> <form id="form1" runat="server"> <div> </div> </form></body></html>
後台Extjs一般處理常式的代碼實現:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace Demo{ /// <summary> /// Extjs 的摘要說明 /// </summary> public class Extjs : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string userName = context.Request.QueryString["userName"]; string userPass = context.Request.QueryString["passWord"]; if (userName == "quwenzhe" && userPass == "123") { context.Response.Write("ok"); //假設驗證成功則返回ok } else { context.Response.Write("fail");//假設驗證失敗則返回fail } context.Response.End(); } public bool IsReusable { get { return false; } } }}
代碼的運行過程:當點擊確定button後,會觸發CheckLogin函數,CheckLogin通過Ajax將擷取的資料傳到Extjs一般處理常式進行檢驗,並擷取Extjs一般處理常式的返回值。假設返回值是ok,則調用loginSuccess函數彈出新視窗;否則,則彈出登入失敗的提示。
執行代碼後,登入頁面例如以下:
登入成功後的例如以下:
到此,用Extjs實現的登入功能已經完畢。此外, 在學習過程中,發現自己存在淺嘗輒止的問題,剛學會一點就以為自己明確了,希望大家能引以為戒,不要犯相同的錯誤。
Ext是個非常好的架構,學好它在非常大程度上能協助我們學習DWZ,希望大家能用心學習這塊。
Asp.Net+Extjs實現登入