我的AjaxXmlHttp

本文介绍了一种利用Ajax技术实现实时用户激活状态更新的方法。通过JavaScript与ASP.NET的结合,无需刷新页面即可完成用户的激活与取消激活操作。
 1// JScript 文件
 2
 3 function AjaxXmlHttp(url)
 4    {
 5        var xmlhttp;
 6                try
 7                {   
 8                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 9                }

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

16                   catch(e)
17                   {
18                        try
19                        {
20                        }

21                        catch(e)
22                        {
23                            xmlhttp = new XMLHttpRequest();
24                        }

25                   }

26                }

27                
28                 xmlhttp.onreadystatechange = function()
29                 {         
30                   if(xmlhttp.readystate==4)
31                    {                  
32                        
33                         if(xmlhttp.status == 200)
34                         {
35                            
36                           alert(xmlhttp.ResponseText);                       
37                         }

38                         else
39                         {
40                            alert("读取数据出错,错误端口号是:"+xmlhttp.status);
41                         }

42                    }

43               }
           
44              xmlhttp.open("get",url,true);
45              xmlhttp.send(null);              
46    }
二、处理xmlhttp
HTML
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Active.aspx.cs" Inherits="Ajax_Active" %>
多余的html代码应除掉。

CS
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class Ajax_Active : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16
17        string id = Request["id"].ToString();
18        string flag = Request["flag"].ToString();
19        if (flag == "no")
20        {
21            string sql = "update userinfo set isactivate='0' where userid='" + id + "'";
22            DAL.SQLHelper.ExecuteNonQuery(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql, null);
23            Response.Write("取消激活成功");
24        }

25        else if (flag == "yes")
26        {
27            string sql = "update userinfo set isactivate='1' where userid='" + id + "'";
28            DAL.SQLHelper.ExecuteNonQuery(DAL.SQLHelper.CONN_STRING, CommandType.Text, sql, null);
29            Response.Write("激活成功");
30        }

31        
32    }

33}
三、调用
 1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MyUsers.aspx.cs" Inherits="u_MyUsers" %>
 2
 3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4<html xmlns="http://www.w3.org/1999/xhtml">
 5<head runat="server">
 6    <title>无标题页</title>
 7    <link href="StyleSheet.css" type="text/css" rel="Stylesheet" />
 8
 9    <script src="../Js/AjaxXmlHttp.js" language="javascript"></script>
10
11</head>
12<body>
13    <form id="form1" runat="server">
14        <div>
15            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
16                <Columns>
17                    <asp:BoundField HeaderText="会员编号" DataField="username" />
18                    <asp:BoundField HeaderText="昵称" DataField="nickname" />
19                    <asp:BoundField HeaderText="姓名" DataField="realname" />
20                    <asp:BoundField HeaderText="手机号" DataField="mobile" />
21                    <asp:BoundField HeaderText="QQ号" DataField="qq" />
22                    <asp:BoundField HeaderText="E-mail" DataField="email" />
23                    <asp:TemplateField HeaderText="状态">
24                        <ItemTemplate>
25                            <%#IsActive(Eval("isactivate").ToString())%>
26                        </ItemTemplate>
27                    </asp:TemplateField>
28                    <asp:TemplateField HeaderText="操作">
29                        <ItemTemplate>
30                            <href="#" onclick="AjaxXmlHttp('../Ajax/Active.aspx?id=<%#Eval("userid") %>&flag=yes');ReLoad();">
31                                激活</a> <href="#" onclick="AjaxXmlHttp('../Ajax/Active.aspx?id=<%#Eval("userid") %>&flag=no');ReLoad();">
32                                    取消激活</a>
33                        </ItemTemplate>
34                    </asp:TemplateField>
35                </Columns>
36            </asp:GridView>
37        </div>
38    </form>
39</body>
40</html>

转载于:https://www.cnblogs.com/htht66/archive/2008/04/10/1147549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值