欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【.NET Ajax的无刷新技术实例详解】,下面是详细的分享!
.NET Ajax的无刷新技术实例详解
该开发包的新版本还没有来得及体验,估计新版本中会方便一些,可能会去掉手动的设置Global.asax的Application_Start事件中加上Ajax.Utility.HandlerPath="ajax";以及其他麻烦的设置。
下载一个.net Ajax开发包,该开发包包括ASP2.0和目前ASP1.1版使用的Ajax,详细地址参见http://ajax.schwarz-interactive.de/,接下来,开始。
1. 新建一个项目,在引用中添加引用Ajax.dll,Ajax.dll位于下载的压缩包里面。
2.建立HttpHandler,在web.config里面加上:
<configuration>
lt;
system.web>
lt;
httpHandlers>
lt;
add verb="POST,GET"
path="ajax/// <summary>
/// Summary description for Methods.
/// </summary>
ublic class DemoMethods
{
[Ajax.AjaxMethod]
ublic string GetCustomerMac(string clientIP)
//这里输入客户端IP,这个函数知识测试用,
你也可以写一个其他的简单一点的函数代替
{
tring mac="";
System.Diagnostics.Process process=
new System.Diagnostics.Process();
rocess.StartInfo.FileName="nbtstat";
rocess.StartInfo.Arguments="-a "+clientIP;
rocess.StartInfo.UseShellExecute=false;
rocess.StartInfo.CreateNoWindow=true;
rocess.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,新建一个名为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在页面上中引用default.js :
lt;
script language="javascript"
src=http://www.chinaz.com/program/2006/1024/"default.js">
</script>
在INPUT的onclick事件中加上
onclick="javascript:GetMac()"
lt;
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";
}
运行看看效果。是不是没有刷新就在服务器端取到客户端的MAC地址?
需要注意的是:该版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath="ajax"; 配置文件web.config必须加上HttpHandler的配置信息!
该开发包的新版本还没有来得及体验,估计新版本中会方便一些,可能会去掉手动的设置Global.asax的Application_Start事件中加上Ajax.Utility.HandlerPath="ajax";以及其他麻烦的设置!
以上所分享的是关于.NET Ajax的无刷新技术实例详解,下面是编辑为你推荐的有价值的用户互动:
相关问题:什么叫做ajax的无刷新?如何实现?
答:最简单的一种方式是,能过JQuery来实现: 例:$.ajax({ url:"a.aspx?a=123", type:"post", success:function(data){ } }); Ajax的原理就是:通过javascript的方式,将前台数据通过xmlhttp对象传递到后台,后台在接收到请求后,将需要的结果,再传回... >>详细
相关问题:jquery实现AJAX无刷新。。有哪些要点。。最好能给...
答:http://www.w3school.com.cn/jquery/jquery_ref_ajax.asp 这里面你想找的代码都有 >>详细
相关问题:asp.net页面局部刷新;求一个用AJAX实现局部刷新的...
答:$.ajax({ type:'get', url:'gettime.ashx', success:function(time){ $('div').text(time); }, error:function(){ $('div').text('服务器内部错误'); }) gettime.ashx中: context.response.write(DateTime.Now.tostring()) 异步获得系统当前时间 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
