.net中的一般處理常式執行個體,.net程式執行個體

來源:互聯網
上載者:User

.net中的一般處理常式執行個體,.net程式執行個體

最近在學習一般處理常式,也學習了一些jQuery的非同步作業,於是就想著親手做一個小的登陸,鍛煉一下自己。

1、首先建立了一個項目LoginDemo,在此基礎上又添加了一個一般處理常式BackLogin.ashx,具體代碼如下

 ///<span style="font-family: Arial, Helvetica, sans-serif;">沒有牽扯到資料庫查詢,在這寫的比較簡單</span>
public class BackLogin : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            string username = context.Request.Form["username"];            string password = context.Request.Form["password"];            if (username == " " || password == " ")            {                context.Response.Write("請填寫使用者名稱或密碼!");            }            else            {                if (username == "001" && password == "001")                   {                    context.Response.Write("success");                }                else                {                    context.Response.Write("使用者名稱或密碼錯誤,請重新登入!");                }            }            context.Response.End();        }        public bool IsReusable        {            get            {                return false;            }        }    }
2、添加一個Web頁面Login.aspx,作為登陸頁面,瀏覽器如下所示

                

3、三種實現方式

1. Form表單形式

    <form action="BackLogin.ashx" method="post">        <input type="text" name="username" value="{num}" />        <input type="text" name="password" />        <input type="submit" value="登陸" />    </form>
2、非同步Post方式

    <script type="text/javascript">        $(function () {            $("#Login").click(function () {                $.post("BackLogin.ashx", { "username": $("#username").val(), "password": $("#password").val() },            function (msg) {                if (msg == "success") {                    alert("登陸成功");                }                else if (msg == "使用者名稱或密碼錯誤,請重新登入!") {                    alert("登入失敗!");                }                else {                    alert("請輸入使用者名稱和密碼!");                }            });            });            // $("form1").submit();        })    </script>

3、非同步ajax

<script type="text/javascript">          $(document).ready(function () {              $("#Login").click(function () {                  $.ajax({                      url: "BackLogin.ashx",                      type: "POST",                      data: "username=" + escape($('#username').val()) + "&password=" + escape($('#password').val()),                      dataType: "text/plain",                      async: "true",                      success: function (msg) {                          var data = eval("(" + msg.d + ")");                          if (data == "success") {                              alert("登陸成功!");                          }                          else {                              alert("登入失敗!");                          }                      }                  });              })          });    </script>

4、運行效果

輸入001,0010使用者名稱、密碼時瀏覽器顯示如下:


5、反思

第一種實現方式不是非同步,不能很好的起到效果,ajax非同步比Post操作更底層。

6、易犯錯誤

1.<input>標籤內的name值與一般處理常式中擷取form表單的name不一致

2.各種{ }()匹配不準確,造成不易察覺的語法錯誤

7、總結

編程式一定要先有思路,在思路的引導下,細心再細心

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.