
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
5.4 由JavaScript来更新UpdatePanel
经过了Single Page架构的震撼教育后,让我们轻松一点谈谈UpdatePanel的更新动作控制,除了可以通过Trigger、Update函数来更新UpdatePanel外,设计师也能通过JavaScript,从客户端要求更新某一个UpdatePanel,这在许多情况下会相当的有用。那具体该如何做呢?说来也很简单,只要在JavaScript调用__doPostback函数,传入要更新的UpdatePanel即可,程序5-10是本例所使用的.aspx源码,程序5-11则是.cs文件。
程序5-10
Samples\5\AdvAjaxDemo\RefreshUpdateWithJavaScript2.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile= "RefreshUpdateWithJavaScript2.aspx.cs" Inherits= "RefreshUpdateWithJavaScript2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <script language=javascript> function confirmRefresh() { if(confirm("do you want refresh?")) { __doPostback("UpdatePanel1","Refresh"); } } </script> <input id="Button1" type="button" value="Refresh Panel" onclick= "confirmRefresh()"/> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
程序5-11
Samples\5\AdvAjaxDemo\RefreshUpdateWithJavaScript2.aspx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class RefreshUpdateWithJavaScript2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (ScriptManager1.IsInAsyncPostback && ScriptManager1.AsyncPostbackSourceElementID == "UpdatePanel1") { Label1.Text = DateTime.Now.ToString(); } } }
很简单不是?