一、 在web.Config中配置
AjaxPro.Dll
<httpHandlers>
            <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro" />
</httpHandlers>
二、在Global.asax
protected void Application_Start(object sender, EventArgs e)
        {
            AjaxPro.Utility.HandlerPath = "ajaxpro";
        }
三、在页面中注册Ajax方法
AjaxPro.Utility.RegisterTypeForAjax(typeof(UserTestWeb.AjaxMe));

四、页面文件代码如下:
<script type="text/javascript">
   
     RecordNumCount=0; //记录条数
     IndexPage=1;       //当前请求页
     ListNum=10;        //每页显示记录数
//遍历页面上所有 checkBox
   var j=0;    //单次选中,双次不选中
  
   function checkboxcheck()
   {
      var inputs = document.all.tags("INPUT");
      for (var i=0; i < inputs.length; i++)
      {
        if (inputs[i].type == "checkbox" )
        {
         if(j%2==0)
         {
              if(inputs[i].checked!=true)
              {
               inputs[i].checked=true;
              }
          }
          else
          {
             if(inputs[i].checked!=false)
              {
               inputs[i].checked=false;
              }
          }
        }
      }
      j++;
   }
  
   //删除选中checkbox的ID集合
    function CheckAll()
    {
      var ids="";
      var inputs = document.all.tags("INPUT");
      for (var i=0; i < inputs.length; i++)
      {
        if (inputs[i].type == "checkbox" )
        {
          if(inputs[i].checked==true)
          {
               if(ids!=null&&parseInt(inputs[i].id)>0)
                    ids=ids+parseInt(inputs[i].id)+",";
           }
         }
       }
       if(ids!=null||ids!="")
       {
            var DeleteInfos=UserTestWeb.AjaxMe.DeleteUserIds(ids);
            if(DeleteInfos.value!=null)
            {
                 document.getElementById("Div1").innerHTML=DeleteInfos.value;
                 Loads(RecordNumCount,tempindexpage,ListNum);//重新加载数据
                 return;
            }
       }
    }
   
   

   
   
    //加载数据
    function Loads(RecordNumCount,IndexPage,ListNum)
    {
         var infos=UserTestWeb.AjaxMe.GetNavBarHtml("Loads",IndexPage,ListNum,RecordNumCount);
         document.getElementById("divbar").innerHTML=infos.value;
         tempindexpage=IndexPage; //全局变量存放当前页
         document.getElementById("Operationinfo").innerHTML="";
         var dt=UserTestWeb.AjaxMe.LoadUseData(RecordNumCount,IndexPage,ListNum);
         if(dt.value!=null)
         {
           var ds=dt.value;
            if(ds != null )
            {           
                for(var i=0; i<ds.Rows.length; i++)
        {
            var id=ds.Rows[i].id;
        var username=ds.Rows[i].AreaID;
        var email=ds.Rows[i].Area;
        document.getElementById("Operationinfo").innerHTML+="<table><tr><td style=width: 23px; height: 21px><input id="+id+" type='checkbox'/></td><td style=width: 100px; height: 21px>"+username+"</td><td style=width: 100px; height: 21px>"+email+"</td></tr></table>";
         }
            }
       }
       return;
    }
   
    //统计记录条数
    function CountTableRecord()
    {
        var RecordNum=UserTestWeb.AjaxMe.TableRecordNum();
        if(RecordNum.value!=null)
          {
             RecordNumCount=RecordNum.value;
             Loads(RecordNumCount,IndexPage,ListNum);
          }
          else
          {
            document.getElementById("Operationinfo").innerHTML="<div color='Red'>无任何记录</div>";
          }  
    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td style="width: 23px; height: 21px">
                    <input id="0" type="checkbox"    onclick="checkboxcheck();" /></td>
                <td style="width: 100px; height: 21px">
        <input id="Button1" type="button" value="加载数据" onclick="CountTableRecord();" /></td>
                <td style="width: 100px; height: 21px"><input id="aaa" type="button" name="删除所有" onclick="CheckAll();" value="删除选中"/>&nbsp;
                </td>
            </tr>
        </table>
        <div id="Operationinfo"></div>
        <div id="Div1"></div>
    </div>
        <div style="z-index: 101; left: 3px; width: 500px; position: absolute; top: 348px;
            height: 1px" id="divbar">
        </div>
    </form>
</body>
</html>


Ajaxme.CS代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Threading;

using UserTest.BLL;
using AjaxPro;

namespace UserTestWeb
{
    public class AjaxMe
    {
        #region 返回当前页加载的数据记录
        [AjaxMethod]
        public static DataTable LoadUseData(int CountAll,int IndexPage,int ListNum )
        {
            DataTable Dt;
            Dt = new DataTable();
            Dt = UserTest.BLL.usert.DtUsers(CountAll, IndexPage, ListNum);
            System.Threading.Thread.Sleep(100);
            return Dt;
        }
        #endregion

        #region 缓存

        //public static object CreateObject(string CacheKey)
        //{
        //    object objType = GetCache(CacheKey);//从缓存读取
        //    if (objType == null)
        //    {
        //        try
        //        {
        //            DataTable Dt = new DataTable();
        //            Dt = UserTest.BLL.usert.DtUsers();
        //            objType = Dt;
        //            SetCache(CacheKey, objType);// 写入缓存
        //        }
        //        catch (System.Exception ex)
        //        { throw (ex); }
        //    }
        //    return objType;
        //}
        ///// <summary>
        ///// 读取CacheKey的Cache值
        ///// </summary>
        ///// <param name="CacheKey"></param>
        //public static object GetCache(string CacheKey)
        //{
        //    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        //    return objCache[CacheKey];
        //}

        ///// <summary>
        ///// 设置当前应用程序指定CacheKey的Cache值
        ///// </summary>
        ///// <param name="CacheKey"></param>
        ///// <param name="objObject"></param>
        //public static void SetCache(string CacheKey, object objObject)
        //{
        //    System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        //    objCache.Insert(CacheKey, objObject);
        //}
        #endregion

        #region 表记录总数
        [AjaxMethod]
        public static int TableRecordNum()
        {
            return UserTest.BLL.usert.CountUsers();
        }
        #endregion

        #region 根据数据返回翻页字符
        [AjaxMethod]
        public static string GetNavBarHtml(string s, int pageIndex, int listNum,int recordCountAll)
        {
            // 计算显示页
            int pageCount = Convert.ToInt32(recordCountAll/listNum);
            int x = pageIndex / listNum;
            if (pageIndex % listNum == 0) --x;
            int startPage = (x * listNum) + 1;
            int endPage = Math.Min(pageCount, startPage + 9);

            // 生成换页代码
            System.Text.StringBuilder sb = new StringBuilder();
            string url = "<A href=# onclick={0}({1},{2},{3})>{4}</A>";

            if (startPage > 1)
            {
                sb.Append(String.Format(url, s, recordCountAll, 1, listNum, "首页"));
                sb.Append("&nbsp");
                sb.Append(String.Format(url, s, recordCountAll, startPage - 1, listNum, "前十页"));
                sb.Append("&nbsp");
            }

            for (int i = startPage; i <= endPage; i++)
            {
                if (i != pageIndex)
                    sb.Append(String.Format(url, s, recordCountAll, i + 1, listNum, i.ToString()));
                else
                    sb.Append(String.Format(url, s, recordCountAll, i, listNum, i.ToString()));

                sb.Append("&nbsp");
            }

            if (pageCount >= endPage)
            {
                sb.Append(String.Format(url, s, recordCountAll, endPage + 1, listNum, "后十页"));
                sb.Append("&nbsp");
                sb.Append(String.Format(url, s, recordCountAll, pageCount, listNum, "末页"));
            }
            System.Threading.Thread.Sleep(100);
            return sb.ToString();
        }
        #endregion

        #region 删除用户选中数据
        [AjaxMethod]
        public static string DeleteUserIds(string ids)
        {
            string returnInfos ="";
            if(ids.Length>0&&!ids.ToString().Equals(null))
            {
                if (false == UserTest.BLL.usert.DeleteUsers(ids))
                {
                    returnInfos = "<font color=Red style=font-size:12px>删除失败!</font>";
                }
                else
                {
                    returnInfos = "<font color=Red style=font-size:12px>操作成功!</font>";
                }
            }
            else
            {
                returnInfos = "<font color=Red style=font-size:12px>您没有选中任何数据!</font>";
            }
            System.Threading.Thread.Sleep(100);
            return returnInfos;
        }
        #endregion

        #region 检测用户
        [AjaxMethod]
        public static string chkUserName(string username)
        {
            string returnInfos = "";
            bool result = UserTest.BLL.usert.isExit(username);
            if (false == result)
            {
                returnInfos = "<font color=Red style=font-size:12px>该用户名已经有人注册过了!</font>";
            }
            else
            {
                returnInfos = "<font color=Red style=font-size:12px>该用户" + username + "可以注册</font>";
            }
            System.Threading.Thread.Sleep(100);
            return returnInfos;
        }

        #endregion
    }
}
 posted on 2008-04-25 19:51  ︷起↘嚸.  阅读(745)  评论(1编辑  收藏  举报