官术网_书友最值得收藏!

  • 決戰.NET
  • 黃忠成
  • 225字
  • 2018-12-27 16:56:05

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>   &nbsp;
        </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();
            }
        }
    }

很簡單不是?

主站蜘蛛池模板: 靖西县| 玉环县| 乐平市| 遵化市| 余干县| 修武县| 轮台县| 定陶县| 旬邑县| 大英县| 大洼县| 博乐市| 苍山县| 正定县| 团风县| 承德县| 石渠县| 行唐县| 五莲县| 吉林省| 治县。| 满城县| 竹山县| 巴彦淖尔市| 霸州市| 保靖县| 安新县| 抚远县| 嘉黎县| 靖州| 霍林郭勒市| 凌云县| 双桥区| 晋州市| 张家界市| 桂东县| 孝感市| 台山市| 潮州市| 名山县| 天峨县|