DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解
RegisterStartupScript:ASP.NET AJAX 的一項實用功能
編輯:AJAX詳解     

最近在進行的專案一直使用 Atlas July CTP。自從 Atlas 正式更名後,接連推出了 Beta 與 Beta 2 兩個版本,但我始終在觀望,原因很單純:Beta 版不夠穩定,升級又太麻煩。某天偶然讀到楊丹的這篇隨筆,才發現原來 ScriptManager 可以在客戶端部分刷新的 UpdatePanel 中註冊啟動後要執行的 JavaScript!這個問題困擾了我很久。

有了它,你不僅能在使用 UpdatePanel 的情況下用 JavaScript 彈出對話框,也能在開發含 JavaScript 腳本的伺服器控制項時,讓控制項的啟動腳本得以執行。原本那些與 UpdatePanel 不相容的控制項,因此可以很輕易地改造成 Ajax 可用的版本。

不過有個地方令人費解:不知道出於什麼考量,RegisterStartupScript 竟然被實作為類別的靜態函數,可少了 ScriptManager 的 UpdatePanel 根本無法執行,這樣反而還得多打幾個字。

另外要特別注意第一個參數是 UpdatePanel 的實例。如果頁面中有多個 UpdatePanel:

  • 當每個 UpdatePanel 的 UpdateMode 都是 Always(預設值)時,你傳入哪一個實例都可以。
  • 當 UpdateMode 設為 Conditional 時,你必須傳入 正在更新的那個 UpdatePanel 實例,腳本才會生效。

頁面標記

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server" />
        <br />

        <asp:UpdatePanel id="UpdatePanel1" UpdateMode="conditional" runat="server">
          <ContentTemplate>
            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
            <asp:LinkButton id="LinkButton1" runat="server">LinkButton</asp:LinkButton>
          </ContentTemplate>
          <Triggers>
            <asp:AsyncPostBackTrigger ControlID="LinkButton1" />
          </Triggers>
        </asp:UpdatePanel>

        <asp:UpdatePanel ID="UpdatePanel2" RenderMode="block" UpdateMode="always" runat="server">
          <ContentTemplate>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:LinkButton ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
          </ContentTemplate>
        </asp:UpdatePanel>

        <br />
        <div>
          <asp:UpdateProgress id="UpdateProgress1" runat="server">
            <ProgressTemplate>
            Please waiting, retrieving data from server
            </ProgressTemplate>
          </asp:UpdateProgress>
        </div>
    </form>
</body>
</html>

後端程式碼

using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
  }

  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    System.Threading.Thread.Sleep(1000);
    TextBox1.Text = DateTime.Now.ToLongTimeString();
    TextBox2.Text = TextBox1.Text;
    string JS = "alert('ok button1 clicked!')";
    Microsoft.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel2, this.GetType(), "btn1clicked", JS, true);
  }

  protected void LinkButton2_Click(object sender, EventArgs e)
  {
    System.Threading.Thread.Sleep(900);
    TextBox2.Text = DateTime.Now.ToLongTimeString();
    TextBox1.Text = TextBox2.Text;
    string JS = "alert('ok button 2 clicked!')";
    Microsoft.Web.UI.ScriptManager.RegisterStartupScript(UpdatePanel2, this.GetType(), "btn2clicked", JS, true);
  }
}

補充一點:使用過程中我發現一個問題,不確定是 Bug 還是我用法不當——在除錯時發現 UpdatePanel.IsInPartialRendering 的值始終是 false,希望有高人能指点迷津。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved