用xmlHttp来做一个异步请求数据里例子

这个东西我想过很久,很早就想试试怎么做的,但是依赖心理太强了,一直都是使用的网络上流传的Ajax.dll 或者AjaxPro.dll或者直接用微软的Atlas到现在的Ajax 1.0里面的控件,感觉有的时候还好,能看到很多特殊的效果,但是在有些小的地方还是不那么方便,看到别人的博客里面写的xmlHttp的AJAX例子他们好像写的很高兴,我也挺羡慕的,禁不住诱惑的我今天上午也写了一个用户注册的时候可以用到的异步验证(这个用ajax.dll里面的AjaxMethod也可以实现的哦):
先要创建两个页面:没有办法,好像一定要创建两个(Reg.aspx,RegCheck.aspx)页面,在Reg.aspx页面中:
 1 < html xmlns = " http://www.w3.org/1999/xhtml "   >
 2 < head runat = " server " >
 3      < title > Untitled Page </ title >
 4      < script >
 5         var xmlhttp = false ;
 6         function GetXmlHttp()
 7          {
 8            try
 9            {
10                 xmlhttp=new ActiveXObject("Msxml 2.XMLHTTP3.0");
11            }

12            catch(e)
13            {
14                try
15                {
16                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
17                }

18                catch(e)
19                {
20                    xmlhttp=false;
21                }

22            }

23                return xmlhttp;
24        }

25         function uid(T)
26          {
27            var id=document.getElementById(T).value;
28            GetXmlHttp();
29            var url="RegCheck.aspx?id="+id;
30            xmlhttp.onreadystatechange=callback;
31            xmlhttp.open("post",url,true);
32            xmlhttp.send();
33        }

34          // 下面的xmlhttp.readyState有4种状态
35         function callback()
36         
37            if(xmlhttp.readystate==1)
38            {
39                alert("readyState");                               
40            }

41            else
42            if(xmlhttp.readyState==4)
43            {
44                 alert(xmlhttp.responseText);
45                
46                 if(xmlhttp.responseText>0)
47                 {
48                    alert("您的用户名被占用了")
49                    xmlhttp=true;
50                 }

51                 else
52                 {
53                    xmlhttp=false;
54                 }

55           }

56            xmlhttp.send(null);
57        }

58     
59      </ script >
60 </ head >
61 < body >
62      < form id = " Form1 "  method = " post "  runat = " server " >
63            < div style = " text-align: center " >
64          < table >
65              < tr >
66                  < td style = " width: 100px " >
67                     用户名: </ td >
68                  < td style = " width: 100px " >
69                      < asp:TextBox ID = " TextBox1 "  runat = " server "  Width = " 89px "  onchange = " uid('TextBox1') " ></ asp:TextBox ></ td >
70                  < td style = " width: 100px " >
71                  </ td >
72              </ tr >
73              < tr >
74                  < td style = " width: 100px " >
75                     密码: </ td >
76                  < td style = " width: 100px " >
77                      < asp:TextBox ID = " TextBox2 "  runat = " server "  Width = " 89px " ></ asp:TextBox ></ td >
78                  < td style = " width: 100px " >
79                  </ td >
80              </ tr >            
81          </ table >
82        </ div >            
83      </ form >
84 </ body >
85 </ html >
这样页面就回跳转到RegCheck.aspx页面去,
 1       protected   void  Page_Load( object  sender, EventArgs e)
 2          {
 3            if (!IsPostBack)
 4            {
 5                Bind();
 6            }

 7        }

 8          private   void  Bind()
 9          {
10            try
11            {   
12                int uid = int.Parse(Request.QueryString["id"].ToString());
13
14                SqlConnection con = new SqlConnection
15                    ("server=.;uid=sa;pwd=sa;datebase=pubs");
16                SqlDataAdapter sda = new SqlDataAdapter
17                    ("select SysNo, CustomerID,Pwd,CustomerName,Phone,CellPhone from Customer where SysNo="+uid, con);
18
19             
20                DataTable dt = new DataTable();
21                sda.Fill(dt);
22                if (dt.Rows.Count > 0)
23                {
24                    Response.Write("1");
25                  //  Response.Write(dt.Rows[0][3].ToString());
26                    Response.End();
27                }

28                else
29                {
30                    Response.Write("0");
31                }

32               
33            }

34            catch(Exception e)
35            {
36                string str = e.ToString(); 
37            }

38        }

上面代码中在执行到Response.End()的时候会抛出一个“事务被强制关闭”的异常,但是没有关系,这个对整个程序没有什么影响的。

你可能感兴趣的:(xmlhttp)