本篇将使用AjaxPro.Net框架实现同样的功能:在客户端异步获取服务端时间!AjaxPro.Net是一个优秀的.net环境下的ajax框架,用法很简单,可以查阅相关资料,简单说有几点:
1)添加AjaxPro.dll应用;
2)配置web.config;
3)写服务端方法,加[AjaxPro.AjaxMethod]申明;
4)客户端调用服务端方法;(ajaxpro最大的优点:客户端js中直接调用服务端方法)
客户端代码:
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
AjaxPro onLoading
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
id
="loadinfo"
style
="visibility:hidden;position:absolute;left:0px;top:0px;background-color:Red;color:White;"
>
Loading
</
div
>
<
input
id
="Button1"
type
="button"
value
="Get ServerTime"
onclick
="javascript:GetTime();void(0)"
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
<
script
type
="text/javascript"
defer
="defer"
>
// loading效果
AjaxPro.onLoading = function(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
{
var a = document.getElementById("loadinfo");
a.style.visibility = b ? "visible" : "hidden";
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
function GetTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
{
// 调用服务端方法
//调用方法:类名.方法名 (参数为指定一个回调函数)
myLoad.GetServerTime(callback);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
function callback(res) //回调函数,显示结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
{
alert(res.value);
}
</
script
>
</
form
>
</
body
>
</
html
>
注: javascript代码放在form内结束处,确保可以访问AjaxPro对象,否则提示AjaxPro未定义;因为在服务端page_load输出 js到页面后才到找到AjaxPro对象;且javascript的defer="defer",页面加载时先不执行此代码。
服务端代码:
public partial class myLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(myLoad)); //注册ajaxPro,括号中的参数是当前的类名
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
[AjaxPro.AjaxMethod] //申明是ajaxPro方法
public string GetServerTime()
{
System.Threading.Thread.Sleep(2000);
return DateTime.Now.ToString();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)