Function Description: when a user enters the user name on the registration page and the mouse focus leaves the input box, go to the data table to verify that the user name already exists. If yes, the system prompts [unavailable]. Otherwise, prompt [available ].
Required knowledge: HTML, JavaScript, C #, ADO. NET, and how to create an AJAX request object, use the open () and send () methods, and the meanings and usage of readyState, status, onreadystatechange, and responseText.
In this example, two pages are shared: UserReg. aspx-user registration page (in fact, static pages can be used) and CheckUserName. aspx-submit the input user name here for verification.
UserReg. aspx front-end code:
Code
1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "UserReg. aspx. cs" Inherits = "UserReg" %>
2
3 <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 5 6 <title> No title page </title>
7
8 <script language = "javascript" type = "text/javascript">
9 <! --
10 function CheckUserName (userName ){
11 var xmlhttp;
12 try {
13 xmlhttp = new ActiveXObject ('msxml2. xmlhttp ');
14}
15 catch (e ){
16 try {
17 xmlhttp = new ActiveXObject ('Microsoft. xmlhttp ');
18}
19 catch (e ){
20 try {
21 xmlhttp = new XMLHttpRequest ();
22}
23 catch (e)
24 {}
25}
26}
27
28 xmlhttp. onreadystatechange = function (){
29 if (xmlhttp. readyState = 4 ){
30 if (xmlhttp. status = 200 ){
31 var showMes = document. getElementById ("showMes ");
32
33 if (xmlhttp. responseText = "1 "){
34 showMes. innerHTML = "unavailable ";
35}
36 else {
37 showMes. innerHTML = "available ";
38}
39}
40}
41
42}
43 xmlhttp. open ("get", "CheckUserName. aspx? UserName = "+ escape (userName ));
44 xmlhttp. send (null );
45}
46
47 // -->
48 </script>
49
50 51 <body>
52 <form id = "form1" runat = "server">
53 <div>
54 user name <asp: TextBox ID = "txtUserName" runat = "server" onblur = "CheckUserName (this. value);"> </asp: TextBox>
55 <span id = "showMes"> </span>
56 <br/>
57 passwords & nbsp; Code <asp: TextBox ID = "txtPassword" runat = "server"> </asp: TextBox>
58 </div>
59 </form>
60 </body>
61 62
UserReg. aspx background code: None
CheckUserName. aspx front-end code (clear all other HTML code and leave only this line ):
Code
1 <% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "CheckUserName. aspx. cs" Inherits = "CheckUserName" %>
2
CheckUserName. aspx background code:
Code
1 using System;
2 using System. Collections;
3 using System. Configuration;
4 using System. Data;
5 using System. Linq;
6 using System. Web;
7 using System. Web. Security;
8 using System. Web. UI;
9 using System. Web. UI. HtmlControls;
10 using System. Web. UI. WebControls;
11 using System. Web. UI. WebControls. WebParts;
12 using System. Xml. Linq;
13 using System. Data. SqlClient;
14
15 public partial class CheckUserName: System. Web. UI. Page
16 {
17 protected void Page_Load (object sender, EventArgs e)
18 {
19 using (SqlConnection conn = new SqlConnection (
20 System. Configuration. ConfigurationManager. configurettings ["ConnStr"])
21 {
22 SqlCommand cmd = new SqlCommand ();
23 cmd. Connection = conn;
24 cmd. CommandText = string. Format (
25 "Select Count (*) From PUsers Where UserName = '{0 }'",
26 Request. QueryString ["userName"]);
27 conn. Open ();
28 if (int. Parse (cmd. ExecuteScalar (). ToString () = 1)
29 {
30 Response. Write ("1 ");
31}
32 else
33 {
34 Response. Write ("2 ");
35}
36}
37}
38}
This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/shuilv2000/archive/2009/02/17/3898801.aspx