jQuery练习(用户登录)

下载地址:

http://download.csdn.net/source/899554

前台

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head> 
  5.     <title>jQueryAjax登录</title> 
  6.     <style type="text/css"> 
  7.         .show{ display:block;} 
  8.         .hide{ display:none;} 
  9.     </style> 
  10.     <script type="text/javascript" src="../js/jquery-1.2.6.js"></script> 
  11.     <script type="text/javascript"> 
  12.         /*********注意********
  13.             如果定义了class即<div class="">则addClass()失效。
  14.             用.css("display","block");取代,可能是优先级问题。
  15.             //$('#wait').addClass("hide"); 
  16.         */
  17.         function myajax(){   
  18.             if(($("#txtname").val()=="")||($("#txtpwd").val()==""))
  19.                 alert('用户名和密码不可为空!');
  20.             else
  21.             {
  22.                 $.ajax({ 
  23.                     type:'post', 
  24.                     url:'AjaxServer.aspx', 
  25.                     data:jsonData(), 
  26.                     dataType:'json', 
  27.                     beforeSend:beforecall, 
  28.                     success:callback 
  29.                 }); 
  30.             }
  31.         } 
  32.         //封装json数据,为了代码清晰 
  33.         function jsonData(){ 
  34.             var jsonStr="({"
  35.             jsonStr+="/"name/":"; 
  36.             jsonStr+="/""+$("#txtname").val()+"/""; 
  37.             jsonStr+=","; 
  38.             jsonStr+="/"pwd/":"; 
  39.             jsonStr+="/""+$("#txtpwd").val()+"/""; 
  40.             jsonStr+="})"; 
  41.             return eval(jsonStr); 
  42.         } 
  43.         //回调前方法
  44.         function beforecall(){ 
  45.             $('#wait').css("display","block"); 
  46.             $('#wait').html('调出中...'); 
  47.         } 
  48.         //回调函数 
  49.         function callback(data){ 
  50.             if(data.sta=="1")
  51.             {
  52.                 $('#wait').html("欢迎你,"+data.name);
  53.                 $('#loginpanel').addClass("hide");
  54.             }
  55.             else
  56.             {
  57.                 $('#wait').css("display","none"); 
  58.                 alert(data.info);
  59.             }
  60.         } 
  61.         $(function(){ 
  62.             $('#confirm').click(myajax); 
  63.         }) 
  64.     </script> 
  65. </head> 
  66. <body> 
  67.     <div id="wait" class="hide">显示进度</div> 
  68.     <div id="loginpanel">
  69.         <input id="txtname" type="text" /><br />
  70.         <input id="txtpwd" type="text" /><br />
  71.         <input id="confirm" type="button" value="登录" />
  72.     </div>
  73. </body> 
  74. </html>

后台

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. public partial class test_AjaxServer : System.Web.UI.Page
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         Hashtable ht = new Hashtable();
  16.         string name = Request.Params["name"].ToString();
  17.         string pwd = Request.Params["pwd"].ToString();
  18.         if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))
  19.         {
  20.             if (Check(name,pwd))//调用业务逻辑
  21.             {
  22.                 ht.Add("info""成功了");
  23.                 ht.Add("sta""1");
  24.             }
  25.             else
  26.             {
  27.                 ht.Add("info""用户名或密码错误");
  28.                 ht.Add("sta""0");
  29.             }
  30.             ht.Add("name", name);
  31.             Response.Write(CreateJsonParams(ht));
  32.         }
  33.         Response.End();
  34.     }
  35.     //构造json数据
  36.     private string CreateJsonParams(Hashtable items)
  37.     {
  38.         string returnStr = "";
  39.         foreach (DictionaryEntry item in items)
  40.         {
  41.             returnStr += "/"" + item.Key.ToString() + "/":/"" + item.Value.ToString() + "/",";
  42.         }
  43.         return "{" + returnStr.Substring(0, returnStr.Length - 1) + "}";
  44.     }
  45.     /// <summary>
  46.     /// 执行业务逻辑
  47.     /// </summary>
  48.     /// <param name="name"></param>
  49.     /// <param name="pwd"></param>
  50.     /// <returns></returns>
  51.     private bool Check(string name, string pwd)
  52.     {
  53.         if ((name == "admin") && (pwd == "admin"))
  54.             return true;
  55.         else
  56.             return false;
  57.     }
  58. }

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值