<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register TagPrefix="Samples" Namespace="MySampleControl" Assembly="MySampleControl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server" > <Scripts> <asp:ScriptReference Assembly="MySampleControl" Name="MySampleControl.UpdatePanelAnimation.js" /> </Scripts> </asp:ScriptManager> <div> <Samples:UpdatePanelAnimationWithClientResource ID="UpdatePanelAnimator1" BorderColor="Green" Animate="true" UpdatePanelID="UpdatePanel1" runat="server" > </Samples:UpdatePanelAnimationWithClientResource> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:Calendar ID="Calendar2" runat="server"> </asp:Calendar> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
注意,第一,因为我们实际在上面创建了一个简单的继承自Control的控件;所以,在工具栏中会出现这个控件—你只要把这个控件直接拖动到示例页面中即可。第二,在上面的代码段中,我们包括了一个<asp:ScriptReference>子元素。此元素引用了你在前一节创建的那个程序集MySampleControl和UpdatePanelAnimation.js文件。ASP.NET AJAX在线教程中有关于这个子元素的细节说明。
最后,按下F5键启动该网站程序。每当你点击日历控件中的某一个日期时,你会看到围绕UpdatePanel控件出现一个绿色的边框。下图3相应于此程序运行时刻的一个快照。
图3.示例网站运行时刻的一个快照。
四、 小结
在本文中,我们实现了把一个JavaScript文件作为一个资源嵌入到一个.NET程序集中。然后,通过一些简单的ASP.NET AJAX网站的例子展示如何在Web应用程序中引用此程序集中嵌入的脚本文件。注意,所有本文中涉及的基本操作是基于定制组件扩展ASP.NET AJAX框架的基本功。
| 第1页: 从Web页面中引用嵌入的脚本文件 |