asp.net ajax中的错误处理

• 服务器端ScriptManager设置
– AllowCustomErrorsRedirect属性:遇到错误是否 自动根据web.config中的设置跳转,默认值为True
– AsyncPostBackError事件:异步刷新中出现错误
时触发的事件。
– AsyncPostBackErrorMessage属性:客户端接受到的错误信息。


客户端编程
– 响应PageRequestManager的endRequest事件。
– 将errorHandled属性设为true。

-------------------------------------------------------------------------------------------------------------------------------------

实例代码:

aspx页面代码

<asp:ScriptManager ID="ScriptManager1" runat="server">
	<Services>
		<asp:ServiceReference Path="ErrorHandling.asmx" />
	</Services>
</asp:ScriptManager>
		
<input type="button" value="getDivision" οnclick="getDivision(5, 0)" />
<input type="button" value="timeout" οnclick="timeout()" />
		
<script language="javascript" type="text/javascript">
	function getDivision(a, b)
	{
		ErrorHandling.GetDivision(a, b, null, failedCallback);
	}
	
	function timeout()
	{
		ErrorHandling.set_timeout(2000);//设置超时时间
		ErrorHandling.Timeout(null, failedCallback);
	}
			
	function failedCallback(error)
	{
		var message = String.format(
			"Timeout: {0}\nMessage: {1}\nExceptionType: {2}\nStackTrace: {3}",
			error.get_timedOut(),
			error.get_message(),
			error.get_exceptionType(),
			error.get_stackTrace());
			
			alert(message);
	}
</script>

ErrorHandling.asmx页面代码

<%@ WebService Language="C#" Class="ErrorHandling" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Threading;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ErrorHandling  : System.Web.Services.WebService
{
	[WebMethod]
	public int GetDivision(int a, int b)
	{
		return a / b;//0不能做为除数
	}

	[WebMethod]
	public int Timeout()
	{
		Thread.Sleep(5000);
		return 0;
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值