使用AjaxPro实现ajax效果
2007-11-25 20:24
507 查看
本篇将使用AjaxPro.Net框架实现同样的功能:在客户端异步获取服务端时间!AjaxPro.Net是一个优秀的.net环境下的ajax框架,用法很简单,可以查阅相关资料,简单说有几点:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
1)添加AjaxPro.dll应用;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2)配置web.config;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3)写服务端方法,加[AjaxPro.AjaxMethod]申明;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4)客户端调用服务端方法;(ajaxpro最大的优点:客户端js中直接调用服务端方法)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
客户端代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>AjaxPro onLoading</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div id="loadinfo" style="visibility:hidden;position:absolute;left:0px;top:0px;background-color:Red;color:White;">Loading</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input id="Button1" type="button" value="Get ServerTime" onclick ="javascript:GetTime();void(0)" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript" defer="defer">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// loading效果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AjaxPro.onLoading = function(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var a = document.getElementById("loadinfo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a.style.visibility = b ? "visible" : "hidden";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function GetTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 调用服务端方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用方法:类名.方法名 (参数为指定一个回调函数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
myLoad.GetServerTime(callback);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function callback(res) //回调函数,显示结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(res.value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
注: javascript代码放在form内结束处,确保可以访问AjaxPro对象,否则提示AjaxPro未定义;因为在服务端page_load输出 js到页面后才到找到AjaxPro对象;且javascript的defer="defer",页面加载时先不执行此代码。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
服务端代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class myLoad : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AjaxPro.Utility.RegisterTypeForAjax(typeof(myLoad)); //注册ajaxPro,括号中的参数是当前的类名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[AjaxPro.AjaxMethod] //申明是ajaxPro方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public string GetServerTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Threading.Thread.Sleep(2000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return DateTime.Now.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
1)添加AjaxPro.dll应用;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2)配置web.config;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
3)写服务端方法,加[AjaxPro.AjaxMethod]申明;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
4)客户端调用服务端方法;(ajaxpro最大的优点:客户端js中直接调用服务端方法)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
客户端代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<html xmlns="http://www.w3.org/1999/xhtml" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<head runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<title>AjaxPro onLoading</title>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</head>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div id="loadinfo" style="visibility:hidden;position:absolute;left:0px;top:0px;background-color:Red;color:White;">Loading</div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<input id="Button1" type="button" value="Get ServerTime" onclick ="javascript:GetTime();void(0)" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
<script type="text/javascript" defer="defer">...
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// loading效果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
AjaxPro.onLoading = function(b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
var a = document.getElementById("loadinfo");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a.style.visibility = b ? "visible" : "hidden";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function GetTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 调用服务端方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//调用方法:类名.方法名 (参数为指定一个回调函数)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
myLoad.GetServerTime(callback);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
function callback(res) //回调函数,显示结果
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
alert(res.value);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
</script>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</body>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</html>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
注: javascript代码放在form内结束处,确保可以访问AjaxPro对象,否则提示AjaxPro未定义;因为在服务端page_load输出 js到页面后才到找到AjaxPro对象;且javascript的defer="defer",页面加载时先不执行此代码。
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
服务端代码:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class myLoad : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
AjaxPro.Utility.RegisterTypeForAjax(typeof(myLoad)); //注册ajaxPro,括号中的参数是当前的类名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[AjaxPro.AjaxMethod] //申明是ajaxPro方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public string GetServerTime()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
System.Threading.Thread.Sleep(2000);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
return DateTime.Now.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
}
相关文章推荐
- 使用AjaxPro实现ajax效果
- 使用AjaxPro实现ajax效果
- 使用AjaxPro实现ajax效果
- 在.net中使用ajaxpro实现ajax效果的步骤
- 使用AjaxPro实现ajax效果
- 使用ajax实现自动提示效果
- 使用script的src实现跨域和类似ajax效果
- 使用script的src实现跨域和类似ajax效果
- 不使用Ajax框架实现Ajax效果?
- 使用ASP.NET AJAX实现幻灯片效果
- [给力原创]使用jQuery-ajax仿新浪微博通知折叠/显示效果,实现数据加载(二)
- [翻译]使用ASP.NET AJAX实现幻灯片效果
- 使用ASP.NET AJAX实现(图片)幻灯片效果
- 使用jQuery和Ajax请求实现分页效果
- [翻译]使用ASP.NET AJAX实现幻灯片效果
- Ajax使用jQuery和PHP实现功能开关效果
- 使用javascript ajax C#实现类似百度的搜索框效果
- 使用Ajax实现无刷新用户名验证和分页的效果(一)
- 使用XMLHttpRequest 实现ajax效果
- 使用jQuery和Ajax请求实现分页效果