您的位置:首页 > 理论基础 > 计算机网络

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

2007-05-24 12:27 357 查看
这个东西我想过很久,很早就想试试怎么做的,但是依赖心理太强了,一直都是使用的网络上流传的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 function uid(T)
26 //下面的xmlhttp.readyState有4种状态
35 function callback()
36
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 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()的时候会抛出一个“事务被强制关闭”的异常,但是没有关系,这个对整个程序没有什么影响的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: