【轉】 ASP.NET使用ICallbackEventHandler無刷新驗證用戶名是否可用

功能說明:當用戶在用戶名輸入框輸入字符並焦點離開此輸入框時,自動到數據庫用戶表中驗證此用戶名是否已被註冊,若是已被註冊,顯示【不可用】,反之,顯示【可用】,期間頁面不刷新,讀者也能夠考慮將提示文字換成圖片等更佳體驗的提示方式。javascript

  (只是的個Demo,沒有考慮諸如Sql注入等問題,期間參考了網上的個別關於ICallbackEventHandler使用的案例。這個Demo是今天在首頁看了某個大蝦關於用ICallbackEventHandler無刷新獲取服務器時間後作的,文章地址忘了,呵呵~,等找到後補上)。html

前臺代碼:java

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ver.aspx.cs" Inherits="Ver" %>
 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 runat="server">
 5      <title>無標題頁</title>
 6  
 7      <script language="javascript">
 8  function VerName() { 
 9   //傳給後臺的參數,在方法RaiseCallbackEvent()中實現
10  var message = document.getElementById("txtName").value; 
11  var context = ""; 
12  <%=sCallBackFunctionInvocation%>;
13 } 
14 function ShowMessage(Mes, context) { 
15   //Mes 是後臺GetCallbackResult()的返回值
16   var spMes =  document.getElementById("spMes");
17   if(Mes == "true")  {
18     spMes.innerHTML = "可用";
19   } 
20   else{
21     spMes.innerHTML = "不可用";
22   }
23   
24 }
25     </script>
26 </head>
27 <body>
28     <form id="form1" runat="server">
29    <div>
30         用戶名:<asp:TextBox ID="txtName" runat="server" onblur="VerName()"></asp:TextBox>
31         <span id="spMes"></span>
32         <br />
33         密&nbsp;&nbsp;&nbsp;&nbsp;碼:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>
34     </div>
35     </form>
36 </html>
View Code

後臺代碼:數據庫

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;

public partial class Ver : System.Web.UI.Page,ICallbackEventHandler
{
    public string sCallBackFunctionInvocation;
    //接收前臺傳入的值
    string userName = "";

    void Page_Load(object sender, System.EventArgs e)
    {
        //註冊腳本到前臺
        sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowMessage", "context");
    }

    #region ICallbackEventHandler 成員

    public string GetCallbackResult()
    {
        using (SqlConnection conn = new SqlConnection(
            System.Configuration.ConfigurationManager.AppSettings["ConnStr"]))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = string.Format(
                "Select Count(*) From PUsers Where UserName='{0}'",
                userName);
            conn.Open();
            if (int.Parse(cmd.ExecuteScalar().ToString()) == 1)
            {
               return "false";
            }
            else
            {
               return "true";
            }
        }
    }
    //接收前臺參數
    public void RaiseCallbackEvent(string eventArgument)
    {
        userName = eventArgument;
    }

    #endregion
}
相關文章
相關標籤/搜索