asp.net ajax程序设计之笔记2--ajax之异常处理

ErrorMessage.aspx 

<style type="text/css">
        #errormsg{
            width:300px;
            height:150px;
            padding:10px;
            border:solid 1px black;
            background:#ffd;
            text-align:left;
            display:none;
            position:absolute;
            left:50px;
            top:50px;
        }
    </style>
    <script type="text/javascript">
    function pageLoad(){
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(onEndRequest);
    }
    function onEndRequest(sender,e){
        if(e.get_error()){
            $get("errormsg").style.display="block";
            $get("lblMsg").innerText=e.get_error().description;
            e.set_errorHandled(true);
        }
    }
    function close1(){
        $get("errormsg").style.display="none";
    }
    </script>

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1"
         runat="server" OnAsyncPostBackError="PostBackError">
        </asp:ScriptManager>
        <asp:Button ID="Button1" runat="server" Text="throw exception" OnClick="btn_click" />
        <div id="errormsg">
            <h3>Error Occurs</h3>
            <p>
                <span id="lblMsg"></span>
            </p>
            <p><input type="button" οnclick="close1()" value="close" />
            </p>
        </div>
        </div>
    </form>

ErrorMessage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager1.RegisterAsyncPostBackControl(Button1);
    }
    protected void btn_click(object sender, EventArgs e)
    {
        throw new Exception("trigger a error!");
    }
    protected void PostBackError(object sender, AsyncPostBackErrorEventArgs e)
    {
        ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值