ASP.NET AJAX的核心- ScriptManager

 

1. ScriptManager成员的分类
  – UpdatePanel的支持成员

  ScriptManager和UpdatePanel控件联合使用可以实现页面异步局部更新的效果。其中的UpdatePanel就是设置页面中异步局部更新区域,它必须依赖于ScriptManager存在,因为ScriptManger控件提供了客户端脚本生成与管理UpdatePanel的功能。
  几个重要的属性:
  1.1 ScriptManager控件的EnablePartialRendering属性:true-实现页面的异步局部更新;false-实现全页面的刷新。

   1.1.1 EnablePageMethods

      ScriptManager的EnablePageMethods属性用于设定客户端javascript直接调用服务端静态WebMethod。示例代码如下:

       EnablePageMethods.aspx:

<script type="text/javascript">
       var txtName;
       var lblMsg;
       function pageLoad(){
           txtName=new Sys.Preview.UI.TextBox($get('txtName'));
           lblMsg=new Sys.Preview.UI.Label($get('lblMsg'));
       }
       function sayHello(){
           PageMethods.SayHello(txtName.get_text(),cb_SayHello);
       }
       function cb_SayHello(result){
           lblMsg.set_text(result);
       }
   </script>
 
<form id="form1" runat="server">
   <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
       <Scripts>
           <asp:ScriptReference Name="PreviewScript.js" Assembly="Microsoft.Web.Preview" />
       </Scripts>
       </asp:ScriptManager>
       <input type="text" id="txtName" />
       <input type="button" value="invoke" onclick="sayHello()" />
       <div id="lblMsg"></div>
   </div>
   </form>

      EnablePageMethods.aspx.cs:

[System.Web.Services.WebMethod]
   public staticString SayHello(string name)
   {
       return "welcome to site " + name;
   }

   1.1.2 EnablePartialRendering

      这个属性用来表示是否激活部分生成。部分生成用来只重新加载部分页面。如果为“真”的话,那么异步控件的常规回发就只显示发送给客户端的增量变化。

   1.1.3 EnableScriptLocalization      

      这个值表示 ScriptManager控件是否呈现本地化版本的脚本档。将 EnableScriptLocalization属性设定为 true时,ScriptManager对象会撷取目前文化特性的脚本档 (如果存在的话)。例如,某个网页可能会指定名为 CustomScript.js 的脚本档。如果文化特性已设定为 fr-CA,则 ScriptManager对象会尝试撷取名为 CustomScript.fr-CA.js 的脚本档

   1.1.4 EnableScriptGlobalization

      该值指示ScriptManager控件是否呈现支持分析区域性特定的信息并设置其格式的脚本。 EnableScriptGlobalization属性设置为 true时,全球化 ECMAScript (JavaScript) 函数(如 Date.localFormat方法)会显示区域性特定的信息。可以在浏览器、服务器代码或网站的配置文件中设置区域性。
  1.2 UpdatePanel控件的RenderMode属性:InLine-UpdatePanel控件被解析成HTML的标记。

  1.3 UpdatePanel控件的UpdateMode属性:Always-UpdatePanel页面上任何一处发生的回发操作都会产生页局部更新;Conditional-只在特定的情况下才产页面的回发,如执行UpdatePanel控件的update()方法或在指定的触发器的操作下。
  1.4 UpdatePanel控件的ChildAsTrigger属性:指示UpdatePanel内部控件引起的回发是否产生当前UpdatePanel控件的局部更新。如果UpdateMode设为Always的话,那ChildAsTrigger局性必须设为True,否则运行出错。


  – 功能控制成员

  • static ScriptManager GetCurrent
  • bool EnablePageMethods { g; s; }
  • bool EnablePartialRendering { g; s; }
  • bool EnableScriptGlobalization { g; s; }
  • bool EnableScriptLocalization { g; s; }
  • IsDebuggingEnabled { g; }
  • bool SupportsPartialRendering { g; s; }

  • AuthenticationServiceManager

  • AuthenticationService { get; }
  • ProfileServiceManager ProfileService { g; }
  • ScriptReferenceCollection Scripts { g; }
  • ServiceReferenceCollection Services { g; }


  – 脚本控件的支持成员

  • void RegisterDispose
  • void RegisterExtenderControl
  • void RegisterScriptControl
  • void RegisterScriptDescriptors


  – 其他成员

2. ScriptMode & ScriptPath
  • ScriptMode
    – 控制脚本类型(调试?发布?)
    – Auto & Inherit
  • ScriptPath
    – 定义了一个脚本加载的基础路径
    – 仅对程序集中的脚本有效
    – %ScriptPath%/%AssemblyVersion%/%DotNet Version%/%Name%.js

3. LoadScriptsBeforeUI
  • 脚本加载会阻塞页面内容呈现
  • 默认情况下ScriptReference会在页面内容前引入
  • 如果把LoadScriptsBeforeUI设为False则会把ScriptReference放在页面末尾加载
  • window.onload事件触发不受任何影响

posted @ 2017-09-18 21:19  tiger_yj  阅读(401)  评论(0编辑  收藏  举报