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

早就听说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里面加上


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

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

None.gif using  System;
None.gif
using  System.Web;
None.gif
None.gif
namespace  AjaxSample
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// Summary description for Methods.
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class DemoMethods
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
InBlock.gif  [Ajax.AjaxMethod]
InBlock.gif  
public string GetCustomerMac(string clientIP) //这里输入客户端IP,这个函数知识测试用,你也可以写一个其他的简单一点的函数代替
ExpandedSubBlockStart.gifContractedSubBlock.gif
  dot.gif
InBlock.gif   
string mac = "";
InBlock.gif
InBlock.gif   System.Diagnostics.Process process 
= new System.Diagnostics.Process();
InBlock.gif   process.StartInfo.FileName 
= "nbtstat";
InBlock.gif   process.StartInfo.Arguments 
= "-a "+clientIP;
InBlock.gif   process.StartInfo.UseShellExecute 
= false;
InBlock.gif   process.StartInfo.CreateNoWindow 
= true;
InBlock.gif   process.StartInfo.RedirectStandardOutput 
= true;
InBlock.gif 
InBlock.gif   process.Start();
InBlock.gif 
InBlock.gif   
string output = process.StandardOutput.ReadToEnd();
InBlock.gif   
int length = output.IndexOf("MAC Address = ");
InBlock.gif
InBlock.gif   
if(length>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    mac 
= output.Substring(length+1417);
ExpandedSubBlockEnd.gif   }

InBlock.gif 
InBlock.gif   process.WaitForExit();
InBlock.gif 
InBlock.gif   
return mac.Replace("-""").Trim();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif
4.写javascript,新建一个名为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>中引用default.js :   <script language="javascript" src="default.js"></script>

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

<INPUT style="Z-INDEX: 101; LEFT: 392px; POSITION: absolute; TOP: 176px" type="button"

    value="客户端获取IP" οnclick="javascript:GetMac();">


6.在page页面的Page_Load事件中加上
None.gif private   void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gif 
{
InBlock.gif   
// 在此处放置用户代码以初始化页面
InBlock.gif
   Ajax.Utility.RegisterTypeForAjax(typeof(AjaxSample.DemoMethods));
ExpandedBlockEnd.gif  }

None.gif

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

(2007-6-23)文章修改:后面第7步部现在的版本不需要了,当时写这篇blog的时候是05年,现在变化已经很多了,推荐使用ajaxPro,
demo下载



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

 

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



 运行看看效果。是不是没有刷新就在服务器端取到客户端的MAC地址??

       需要注意的是:该版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath = "ajax"; 配置文件web.config必须加上HttpHandler的配置信息!

       该开发包的新版本还没有来得及体验,估计新版本中会方便一些,可能会去掉手动的设置Global.asax的Application_Start事件中加上Ajax.Utility.HandlerPath = "ajax";以及其他麻烦的设置!期待ing……


 

转载于:https://www.cnblogs.com/jintan/archive/2005/09/19/239637.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值