首页 - Ajax专区 - 实例应用

初次体验.net Ajax无刷新技术(.net)

发布时间: 2007-03-19 12:35    作者: 未知    来源: 未知    浏览:    评论

上一页 1 2 下一页
   早就听说Ajax技术了,传说中是一种很牛的东西,号称无刷新,其实是在web上通过javascript,使用异步的xmlhttp请求,实现无刷新的web界面。可惜一直没有体验过, 先后听做PHP的朋友用过PHP的Ajax开发包,而且做了很多很酷的东西,使小生羡慕不已。

      今天下了一个.net Ajax开发包,该开发包包括ASP2.0和目前ASP1.1版使用的Ajax,详细地址参见http://ajax.schwarz-interactive.de/,接下来,开工。

 1. 新建一个项目,在引用中添加引用Ajax.dll,Ajax.dll位于下载的压缩包里面。

2.建立HttpHandler,在web.config里面加上

<configuration>
  <system.web>
    <httpHandlers>
    <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax" />
    </httpHandlers> 
    ...
  <system.web>
</configuration>

3.新建一个类DemoMethods,这个类实现获取客户端MAC地址:

using System;
using System.Web;

namespace AjaxSample
{
 /// <summary>
 /// Summary description for Methods.
 /// </summary>
 public class DemoMethods
 {
 
  [Ajax.AjaxMethod]
  public string GetCustomerMac(string clientIP) //para IP is the client's IP
  {
   string mac = "";

   System.Diagnostics.Process process = new System.Diagnostics.Process();
   process.StartInfo.FileName = "nbtstat";
   process.StartInfo.Arguments = "-a "+clientIP;
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.CreateNoWindow = true;
   process.StartInfo.RedirectStandardOutput = true;
 
   process.Start();
 
   string output = process.StandardOutput.ReadToEnd();
   int length = output.IndexOf("MAC Address = ");

   if(length>0)
   {
    mac = output.Substring(length+14, 17);
   }
 
   process.WaitForExit();
 
   return mac.Replace("-", "").Trim();
  }
 }

}

4.写javascript,新建一个名为http://www.okajax.com/info/example/default.js文件如下,
function GetMac()
{
 var clientIP="192.168.0.1";
 document.getElementById("Mac").value=DemoMethods.GetCustomerMac(clientIP).value
 alert(DemoMethods.GetCustomerMac(clientIP).value);
}

5.在某个Aspx页面放上一个html 的button

在页面上<head>中引用http://www.okajax.com/info/example/default.js :   <script language="javascript" src="http://www.okajax.com/info/example/default.js"></script>

在INPUT的onclick事件中加上onclick="javascript:GetMac()"

<INPUT style="Z-INDEX: 101; LEFT: 392px; POSITION: absolute; TOP: 176px" type="button"
    value="客户端获取IP" onclick="javascript:GetMac();">

6.在page页面的Page_Load事件中加上

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   Ajax.Utility.RegisterTypeForAjax(typeof(AjaxSample.DemoMethods));
  }

         注意:typeof(AjaxSample.DemoMethods)中,AjaxSample是命名空间,DemoMethods是要包含要调用方法的类,即上面第3步.新建类DemoMethods

7.修改Global.asax的Application_Start事件,设置Ajax的HandlerPath :

protected void Application_Start(Object sender, EventArgs e)
  {
     Ajax.Utility.HandlerPath = "ajax";

上一页 1 2 下一页

TAG

Smile Big Smile Surprise Stick out tongue Wink Sad Tongue Tied Indifferent Crying Embarrassed Cool Angry Angel Devil [8-|] [:#] [:-*] [:^)] [<:o)] [|-)] Yes Beer Left Hug Music Star Time Snail Pizza Automobile Umbrella Computer Storm [mo] [8o|] [^o)] [+o(] [*-)] [8-)] Coffee No Drinks [Z] Right Hug Cake Broken Heart Gift Wilted Flower Movie Dog Idea Sleep Email Travel Paradise
呢称:

加粗 斜体 下划线 链接 图片 代码 邮件地址 引用 列表

最多只能输入100个字符