RSS
热门关键字:  Linux  图形  项目管理  LAMP  java
当前位置 : 主页>开源技术>AJAX技术>列表

ASP.NET AJAX入门系列(2):使用ScriptManager控件

来源:中国开源社区 作者:sherman 时间:2007-09-18 点击:

三.客户端脚本模式 字串5

在前面我们提到了ScriptMode属性指定ScriptManager发送到客户端的脚本的模式,它有四种模式:AutoInheritDebugRelease,默认值为Auto 字串4

1Auto:它会根据Web站点的Web.config配置文件来决定使用哪一种模式,只有当配置文件中retail属性设置为false:Inherit:应该是通过程序设置ScriptMode的时候,等同于Auto?(不太了解)

字串9

<system.web>

  
<deployment retail="false" />
字串9
</system.web>

或者页面中的Debug指令设为true的时候会使用Debug版本,其他的情况都会使用Release版本。 字串6

<%@ Page Language="C#" Debug="true"

AutoEventWireup
="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

2

字串9

3Debug:客户端脚本使用Debug版本,除非retail属性设为true

字串3

4Release:客户端脚本使用Release版本,除非retail属性设为false 字串4

 

字串4

四.错误处理 字串1

在页面回传时如果发生了异常AsyncPostBackError事件将被触发,错误信息的处理依赖于AllowCustomErrors属性、AsyncPostBackErrorMessage属性和Web.config中的<customErrors>配置区。下面看一个简单的错误处理例子,在AsyncPostBackError事件中捕获到异常信息并设置AsyncPostBackErrorMessage属性。

字串1

字串7

<%@ Page Language="C#" %> 字串1

<script runat="server"> 字串7

    protected 
void ErrorProcessClick_Handler(object sender, EventArgs e)

    
{
字串5

        
// This handler demonstrates an error condition. In this example

        
// the server error gets intercepted on the client and an alert is shown. 
字串9

        
throw new ArgumentException();
    }


    protected 
void SuccessProcessClick_Handler(object sender, EventArgs e)
字串5
    
{

字串5


        
// This handler demonstrates no server side exception.

        UpdatePanelMessage.Text 
= "The asynchronous postback completed successfully.";
字串3


    }


    protected 
void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)

    
{ 字串6
        ScriptManager1.AsyncPostBackErrorMessage 
= "异常信息为:" + e.Exception.Message;

    }

字串5


</script>


<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

字串3



    
<title>PageRequestManager endRequestEventArgs Example</title>

    
<style type="text/css"> 字串3

    body 
{ 字串4

        font-family
: Tahoma;

    
}


    #AlertDiv
{ 字串6

    left
: 40%; top: 40%;

    position
: absolute; width: 200px; 字串1

    padding
: 12px; 

    border
: #000000 1px solid;

    background-color
: white; 

字串3



    text-align
: left;

    visibility
: hidden;

    z-index
: 99;

字串6



    
}


    #AlertButtons
{ 字串4

    position
: absolute;

    right
: 5%;

    bottom
: 5%;

字串1



    
}


    
</style>

</head>

<body id="bodytag">

字串6



    
<form id="form1" runat="server">

        
<div>

            
<asp:ScriptManager ID="ScriptManager1" runat="server" 
字串7

            OnAsyncPostBackError
="ScriptManager1_AsyncPostBackError">

            
</asp:ScriptManager>
 
            
<script type="text/javascript" language="javascript">

字串8



                
var divElem = 'AlertDiv';

                
var messageElem = 'AlertMessage';

字串5



                
var errorMessageAdditional = 'Please try again.';

                
var bodyTag = 'bodytag';

字串7


                Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

                
function ToggleAlertDiv(visString)
                
{ 字串4
                     
if (visString == 'hidden')

                     
{ 字串2
                         $get(bodyTag).style.backgroundColor 
= 'white';                         
                     }

                     
else
字串7

                     
{ 字串2
                         $get(bodyTag).style.backgroundColor 
= 'gray';                         

                     }


                     
var adiv = $get(divElem); 字串6

                     adiv.style.visibility 
= visString;

                }


                
function ClearErrorState() {
字串3


                     $get(messageElem).innerHTML 
= '';

                     ToggleAlertDiv('hidden');                     

                }
字串2

                
function EndRequestHandler(sender, args)

                
{ 字串5
                   
if (args.get_error() != undefined && args.get_error().httpStatusCode == '500')
                   
{

字串5



                       
var errorMessage = args.get_error().message

                       args.set_errorHandled(
true);

字串2



                       ToggleAlertDiv('visible');

                       $get(messageElem).innerHTML 
= '"' + 

                                errorMessage + '
" ' + errorMessageAdditional;
字串4


                   }


                }


            
</script>

字串8


            
<asp:UpdatePanel runat="Server" UpdateMode="Conditional" ID="UpdatePanel1">

                
<ContentTemplate> 字串3

                    
<asp:Panel ID="Panel1" runat="server" GroupingText="Update Panel">

                        
<asp:Label ID="UpdatePanelMessage" runat="server" /> 字串1

                        
<br />

                        Last update:

                        
<%= DateTime.Now.ToString() %>

字串9



                        .

                        
<br />

                        
<asp:Button runat="server" ID="Button1" Text="Submit Successful Async Postback" 字串8

                            OnClick
="SuccessProcessClick_Handler" OnClientClick="ClearErrorState()" />

                        
<asp:Button runat="server" ID="Button2" Text="Submit Async Postback With Error" 字串9

                            OnClick
="ErrorProcessClick_Handler" OnClientClick="ClearErrorState()" />

                        
<br /> 字串9

                    
</asp:Panel>

                
</ContentTemplate>

            
</asp:UpdatePanel> 字串1

            
<div id="AlertDiv">

                
<div id="AlertMessage">
字串7

                
</div>

                
<br />

                
<div id="AlertButtons" >

字串9



                    
<input id="OKButton" type="button" value="OK" 

                           runat
="server" onclick="ClearErrorState()" />
字串4


                
</div>

           
</div>

    
</form>

字串9



</body>

</html>

运行后时界面:

字串9

字串2

发生异常信息:

字串7

字串6

 

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册