ASP.NET - UpdatePanel 控件概述

使用 ASP.NET UpdatePanel 控件可生成功能豐富的、以客戶端爲中心的 Web 應用程序。通過使用 UpdatePanel 控件,可以刷新頁的選定部分,而不是使用回發刷新整個頁面。這稱爲執行“部分頁更新”。包含一個 ScriptManager 控件和一個或多個 UpdatePanel 控件的 ASP.NET 網頁可自動參與部分頁更新,而不需要自定義客戶端腳本。

 

一、介紹

      UpdatePanel 控件通過指定頁中無需刷新整個頁面即可更新的區域發揮作用。此過程由 ScriptManager 服務器控件和客戶端 PageRequestManager 類來協調。當啓用部分頁更新時,控件可以通過異步方式發佈到服務器。異步回發的行爲與常規回發類似:生成的服務器頁執行完整的頁和控件生命週期。不過,通過使用異步回發,可將頁更新限制爲包含在 UpdatePanel 控件中並標記爲要更新的頁區域。服務器僅將受影響的元素的 HTML 標記發送到瀏覽器。在瀏覽器中,客戶端 PageRequestManager 類執行文檔對象模型 (DOM) 操作以將現有 HTML 替換爲更新的標記。

      說明: 使用異步回發或使用 XMLHTTPRequest 對象時,如果 URL 包含雙字節字符,則可能發生回發錯誤。此問題可以通過下面的方法得到解決:在頁面的 head 元素中加入 <base href="url"/> 元素,其中 href 屬性設置爲引用該頁面的 URL 編碼的字符串。可以添加動態添加到服務器代碼中的此元素。

 

下圖演示一個首次加載的頁和一個後續異步回發,該回發將刷新 UpdatePanel 控件的內容。

1.1、部分頁呈現概述

 

1.2、啓用部分頁更新

UpdatePanel 控件在網頁中需要 ScriptManager 控件。默認情況下,將啓用部分頁更新,因爲 ScriptManager 控件的 EnablePartialRendering 屬性的默認值爲 true

下面的示例顯示用於定義頁面上的 ScriptManager 控件和 UpdatePanel 控件的標記。UpdatePanel 控件包含一個 Button 控件,當單擊該控件時,將刷新面板中的內容。默認情況下,ChildrenAsTriggers 屬性爲 true。因此,Button 控件將用作異步回發控件。

<asp:ScriptManager ID="ScriptManager"

                   runat="server" />

<asp:UpdatePanel ID="UpdatePanel1"

                 UpdateMode="Conditional"

                 runat="server">

    <ContentTemplate>

       <fieldset>

       <legend>UpdatePanel content</legend>

        <!-- Other content in the panel. -->

        <%=DateTime.Now.ToString() %>

        <br />

        <asp:Button ID="Button1"

                    Text="Refresh Panel"

                    runat="server" />

        </fieldset>

    </ContentTemplate>

</asp:UpdatePanel>

 

1.3、指定 UpdatePanel 控件內容

可以通過聲明方式向 UpdatePanel 控件添加內容,也可以在設計器中通過使用 ContentTemplate 屬性來添加內容。在標記中,將此屬性作爲 ContentTemplate 元素公開。若要以編程方式添加內容,請使用 ContentTemplateContainer 屬性。

當首次呈現包含一個或多個 UpdatePanel 控件的頁時,將呈現 UpdatePanel 控件的所有內容並將這些內容發送到瀏覽器。在後續異步回發中,可能會更新各個 UpdatePanel 控件的內容。更新將與面板設置、導致回發的元素以及特定於每個面板的代碼有關。

 

1.4、指定 UpdatePanel 觸發器

默認情況下,UpdatePanel 控件內的任何回發控件都將導致異步回發並刷新面板的內容。但是,也可以配置頁上的其他控件來刷新 UpdatePanel 控件。可以通過爲 UpdatePanel 控件定義觸發器來做到這一點。觸發器是一類綁定,用於指定使面板更新的回發控件和事件。當引發觸發器控件的指定事件(例如,按鈕的 Click 事件)時,將刷新更新面板。

 

下面的示例演示如何爲 UpdatePanel 控件指定觸發器。

<asp:Button ID="Button1"

            Text="Refresh Panel"

            runat="server" />

<asp:ScriptManager ID="ScriptManager1"

                   runat="server" />

<asp:UpdatePanel ID="UpdatePanel1"

                 UpdateMode="Conditional"

                 runat="server">

                 <Triggers>

                   <asp:AsyncPostBackTrigger ControlID="Button1" />

                 </Triggers>

                 <ContentTemplate>

                 <fieldset>

                 <legend>UpdatePanel content</legend>

                 <%=DateTime.Now.ToString() %>

                 </fieldset>

                 </ContentTemplate>

</asp:UpdatePanel>

      使用 UpdatePanel 控件的 Triggers 元素內的 asp:AsyncPostBackTrigger 元素定義觸發器。(如果在 Visual Studio 中編輯頁面,則可以使用“UpdatePanelTrigger 集合編輯器”對話框創建觸發器。)

觸發器的控件事件是可選的。如果不指定事件,則觸發器事件是控件的默認事件。例如,對於 Button 控件,默認事件是 Click 事件。

 

1.5、如何刷新 UpdatePanel 控件

下面的列表描述 UpdatePanel 控件的屬性設置,這些設置確定在部分頁呈現過程中何時刷新面板的內容。

如果 UpdateMode 屬性設置爲 Always,則 UpdatePanel 控件的內容在每次從頁上的任意位置執行回發時都會更新。這包括來自其他 UpdatePanel 控件內部的控件的異步回發,也包括來自 UpdatePanel 控件外部的控件的回發。

如果 UpdateMode 屬性設置爲 Conditional,則 UpdatePanel 控件的內容在出現以下情況下之一時更新:

·當回發由 UpdatePanel 控件的觸發器引起時。

·當顯式調用 UpdatePanel 控件的 Update 方法時。

·當 UpdatePanel 控件嵌套在另一個 UpdatePanel 控件內並更新父面板時。

·當 ChildrenAsTriggers 屬性設置爲 true 並且 UpdatePanel 控件的子控件導致回發時。嵌套的 UpdatePanel 控件的子控件不會導致更新外部 UpdatePanel 控件,除非將它們顯式定義爲父面板的觸發器。

如果 ChildrenAsTriggers 屬性設置爲 false UpdateMode 屬性設置爲 Always,則將引發異常。此 ChildrenAsTriggers 屬性僅在 UpdateMode 屬性設置爲 Conditional 時使用。

 

1.6、在母版頁中使用 UpdatePanel 控件

若要在母版頁中使用 UpdatePanel 控件,必須決定如何包含 ScriptManager 控件。如果在母版頁上包含 ScriptManager 控件,則可將其用作所有內容頁的 ScriptManager 控件。(如果要以聲明方式在內容頁中註冊腳本或服務,可將 ScriptManagerProxy 控件添加到該內容頁。)

如果母版頁不包含 ScriptManager 控件,則可以將 ScriptManager 控件單獨放置在包含 UpdatePanel 控件的每個內容頁上。設計選擇取決於您計劃如何管理應用程序中的客戶端腳本。有關如何管理客戶端腳本的更多信息,請參見 ScriptManager 控件概述。

在某些情況下,ScriptManager 控件位於母版頁上,對於某個內容頁,不需要使用部分頁呈現功能。在上述情況下,對於相應的內容頁,必須以編程方式將 ScriptManager 控件的 EnablePartialRendering 屬性設置爲 false

 

下面的示例顯示用於母版頁上的 ScriptManager 控件和內容頁上的 UpdatePanel 控件的標記。在本示例中,將在母版頁上定義一個名爲 LastUpdate 的屬性,並從 UpdatePanel 控件內部引用該屬性。

<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

 

    public DateTime LastUpdate

    {

        get

        {

            return (DateTime)(ViewState["LastUpdate"] ?? DateTime.Now);

        }

        set

        {

            ViewState["LastUpdate"] = value;

        }

    }

 

 

    protected void MasterButton2_Click(object sender, EventArgs e)

    {

        LastUpdate = DateTime.Now;

        ((UpdatePanel)ContentPlaceHolder1.FindControl("UpdatePanel1")).Update();

 

    }

 

    protected void Page_Load(object sender, EventArgs e)

    {

        ScriptManager1.RegisterAsyncPostBackControl(Button2);

    }

</script>

 

<html >

<head id="Head1" runat="server">

    <title>ScriptManager in Master Page Example</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ScriptManager ID="ScriptManager1" runat="server" />

            <asp:Panel ID="MasterPanel1" runat="server" GroupingText="Master Page">

               <asp:Button ID="Button1" runat="server" Text="Full Page Refresh" />

               <asp:Button ID="Button2" runat="server" Text="Refresh Panel" OnClick="MasterButton2_Click" />

            </asp:Panel>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>

        </div>

    </form>

</body>

</html>

 

<%@ Page Language="C#" MasterPageFile="MasterCS.master"

    Title="ScriptManager in Content Page" %>

 

<%@ MasterType VirtualPath="MasterCS.master" %>

 

<script runat="server">

 

    protected void Button3_Click(object sender, EventArgs e)

    {

        Master.LastUpdate = DateTime.Now;

    }

 

</script>

 

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1"

    runat="Server">

    <asp:Panel ID="Panel2"

               GroupingText="ContentPage"

               runat="server" >

        <asp:UpdatePanel ID="UpdatePanel1"

                         UpdateMode="Conditional"

                         runat="server">

            <ContentTemplate>

                <p>

                    Last updated: <strong>

                        <%= Master.LastUpdate.ToString() %>

                    </strong>

                </p>

                <asp:Button ID="Button3"

                            Text="Refresh Panel"

                            OnClick="Button3_Click"

                            runat="server"  />

            </ContentTemplate>

        </asp:UpdatePanel>

    </asp:Panel>

</asp:Content>

 

1.7、使用嵌套的 UpdatePanel 控件

UpdatePanel 控件可以嵌套。如果刷新父面板,則也會刷新所有嵌套的面板。

下面的示例顯示用於在一個 UpdatePanel 控件內定義另一個 UpdatePanel 控件的標記。父面板中的按鈕會同時觸發父面板和子面板中的內容更新;而子面板中的按鈕僅觸發子面板的更新。

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

</script>

 

<html >

<head id="Head1" runat="server">

    <title>UpdatePanelUpdateMode Example</title>

    <style type="text/css">

    div.NestedPanel

    {

      position: relative;

      margin: 2% 5% 2% 5%;

    }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ScriptManager ID="ScriptManager"

                               runat="server" />

            <asp:UpdatePanel ID="OuterPanel"

                             UpdateMode="Conditional"

                             runat="server">

                <ContentTemplate>

                    <div>

                        <fieldset>

                            <legend>Outer Panel </legend>

                            <br />

                            <asp:Button ID="OPButton1"

                                        Text="Outer Panel Button"

                                        runat="server" />

                            <br />

                            Last updated on

                            <%= DateTime.Now.ToString() %>

                            <br />

                            <br />

                            <asp:UpdatePanel ID="NestedPanel1"

                                               UpdateMode="Conditional"

                                               runat="server">

                                <ContentTemplate>

                                    <div class="NestedPanel">

                                        <fieldset>

                                            <legend>Nested Panel 1</legend>

                                            <br />

                                            Last updated on

                                            <%= DateTime.Now.ToString() %>

                                            <br />

                                            <asp:Button ID="NPButton1"

                                                        Text="Nested Panel 1 Button"

                                                        runat="server" />

                                        </fieldset>

                                    </div>

                                </ContentTemplate>

                            </asp:UpdatePanel>

                        </fieldset>

                    </div>

                </ContentTemplate>

            </asp:UpdatePanel>

        </div>

    </form>

</body>

</html>

      下面的示例演示一個帶有 GridView 控件的嵌套的 UpdatePanel 控件。GridView 控件位於 UpdatePanel 控件內,且每個 GridView 行包含嵌套在另一個 UpdatePanel 控件內的 GridView 控件。

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html  >

<head runat="server">

    <title>Browse Departments</title>

    <script runat="server">

        protected void DepartmentsGridView_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                SqlDataSource s = (SqlDataSource)e.Row.FindControl("EmployeesDataSource");

                System.Data.DataRowView r = (System.Data.DataRowView)e.Row.DataItem;

                s.SelectParameters["DepartmentID"].DefaultValue = r["DepartmentID"].ToString();

            }

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true" />

        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">

            <ContentTemplate>

        <asp:GridView ID="DepartmentsGridView" runat="server" AllowPaging="True" AllowSorting="True"

            AutoGenerateColumns="False" CellPadding="4" DataSourceID="DepartmentDataSource"

            ForeColor="#333333" GridLines="None" PageSize="3" OnRowDataBound="DepartmentsGridView_RowDataBound">

            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />

            <Columns>

                <asp:BoundField DataField="GroupName" HeaderText="Division" SortExpression="GroupName" >

                    <ItemStyle Width="200px" />

                </asp:BoundField>

                <asp:BoundField DataField="Name" HeaderText="Department Name" SortExpression="Name" >

                    <ItemStyle Width="160px" />

                </asp:BoundField>

                <asp:TemplateField HeaderText="Employees">

                    <ItemTemplate>

                        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">

                            <ContentTemplate>

                                <asp:GridView ID="EmployeesGridView" runat="server" AllowPaging="True" AllowSorting="True"

                                    AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None"

                                    BorderWidth="1px" CellPadding="3" DataSourceID="EmployeesDataSource" GridLines="Vertical"

                                    PageSize="4">

                                    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

                                    <Columns>

                                        <asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" >

                                            <ItemStyle Width="80px" />

                                        </asp:BoundField>

                                        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" >

                                            <ItemStyle Width="160px" />

                                        </asp:BoundField>

                                    </Columns>

                                    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />

                                    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

                                    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />

                                    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

                                    <AlternatingRowStyle BackColor="Gainsboro" />

                                </asp:GridView>

                                <asp:Label runat="server" ID="InnerTimeLabel"><%=DateTime.Now %></asp:Label>

                                <asp:SqlDataSource ID="EmployeesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"

                                    SelectCommand="SELECT HumanResources.EmployeeDepartmentHistory.DepartmentID,

                                                          HumanResources.vEmployee.EmployeeID,

                                                          HumanResources.vEmployee.FirstName,

                                                          HumanResources.vEmployee.LastName

                                                   FROM HumanResources.EmployeeDepartmentHistory

                                                   INNER JOIN HumanResources.vEmployee

                                                     ON HumanResources.EmployeeDepartmentHistory.EmployeeID = HumanResources.vEmployee.EmployeeID

                                                   WHERE HumanResources.EmployeeDepartmentHistory.DepartmentID = @DepartmentID

                                                   ORDER BY HumanResources.vEmployee.LastName ASC, HumanResources.vEmployee.FirstName ASC">

                                    <SelectParameters>

                                      <asp:Parameter Name="DepartmentID" DefaultValue="0" Type="int32" />

                                    </SelectParameters>

                                </asp:SqlDataSource>

                            </ContentTemplate>

                        </asp:UpdatePanel>

                    </ItemTemplate>

                    <ItemStyle Height="170px" Width="260px" />

                </asp:TemplateField>

            </Columns>

            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" VerticalAlign="Top" />

            <EditRowStyle BackColor="#999999" />

            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />

            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />

            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />

            <AlternatingRowStyle BackColor="White" ForeColor="#284775" VerticalAlign="Top" />

        </asp:GridView>

        <asp:Label runat="server" ID="OuterTimeLabel"><%=DateTime.Now %></asp:Label>

            </ContentTemplate>

        </asp:UpdatePanel>

        &nbsp;

        <asp:SqlDataSource ID="DepartmentDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"

            SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department ORDER BY Name">

        </asp:SqlDataSource>

 

    </div>

    </form>

</body>

</html>

      當內部 GridView 控件顯示一頁新記錄時,不會刷新外部面板和外部 GridView 控件的其他行中的面板。當外部 GridView 控件顯示一頁新記錄時,外部面板和嵌套的面板都會刷新。

 

1.8、以編程方式刷新 UpdatePanel

下面的示例演示如何以編程方式刷新 UpdatePanel 控件。在本示例中,頁通過調用 RegisterAsyncPostBackControl 方法將一個控件註冊爲觸發器。代碼通過調用 Update 方法以編程方式刷新 UpdatePanel 控件。

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

 

    protected SortedList AnsweredQuestions

    {

        get { return (SortedList)(ViewState["AnsweredQuestions"] ?? new SortedList()); }

        set { ViewState["AnsweredQuestions"] = value; }

    }

 

    protected void Page_Load()

    {

        ScriptManager1.RegisterAsyncPostBackControl(SurveyDataList);

    }

 

    protected void ChoicesRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)

    {

        SortedList answers = this.AnsweredQuestions;

        RadioButtonList r = (RadioButtonList)sender;

        answers[r.ToolTip] = r.SelectedValue;

        this.AnsweredQuestions = answers;

 

        ResultsList.DataSource = this.AnsweredQuestions;

        ResultsList.DataBind();

 

        if (this.AnsweredQuestions.Count == SurveyDataList.Items.Count)

            SubmitButton.Visible = true;

 

        UpdatePanel1.Update();

    }

 

    protected void SubmitButton_Click(object sender, EventArgs e)

    {

        // Submit responses.

    }

</script>

 

<html >

<head id="Head1" runat="server">

    <title>Registering Controls as Async Postback Controls</title>

    <style type="text/css">

    .AnswerFloatPanelStyle {

    background-color: bisque;

    position: absolute;

    right: 10px;

    height: 130px;

    width: 150px;

    border-right: silver thin solid; border-top: silver thin solid;

    border-left: silver thin solid; border-bottom: silver thin solid;   

    }

    </style>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ScriptManager ID="ScriptManager1" runat="server" />

            <div id="AnswerFloatPanel" class="AnswerFloatPanelStyle" runat="server">

                <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">

                    <ContentTemplate>

                        Completed Questions:

                        <asp:DataList ID="ResultsList" runat="server">

                            <ItemTemplate>

                                <asp:Label ID="ResultQuestion" runat="server" Text='<%# Eval("Key") %>' />

                                ::

                                <asp:Label ID="ResultAnswer" runat="server" Text='<%# Eval("Value") %>' />

                            </ItemTemplate>

                        </asp:DataList>

                        <p style="text-align: right">

                            <asp:Button ID="SubmitButton" Text="Submit" runat="server" Visible="false"

                                OnClick="SubmitButton_Click" />

                        </p>

                        <asp:Label ID="Message" runat="Server" />

                    </ContentTemplate>

                </asp:UpdatePanel>

            </div>

 

            <asp:XmlDataSource ID="SurveyDataSource"

                               runat="server"

                               XPath="/Questions/Question"

                               DataFile="~/App_Data/SurveyQuestions.xml"/>

            <asp:DataList

                ID="SurveyDataList"

                DataSourceID="SurveyDataSource"

                runat="server">

 

                <ItemTemplate>

                  <table cellpadding="2" cellspacing="2">

                    <tr>

                      <td valign="top">

                        <asp:Label id="QuestionLabel" Text='<%# XPath("@Title")%>' runat="server" />

                      </td>

                    </tr>

                    <tr><td>

                      <asp:RadioButtonList ID="ChoicesRadioButtonList" runat="server"

                        DataSource='<%#XPathSelect("Choices/Choice") %>'

                        DataTextField="InnerText" DataValueField="InnerText"

                        AutoPostBack="True"

                        ToolTip='<%# "Question" + XPath("@ID") %>'

                        OnSelectedIndexChanged="ChoicesRadioButtonList_SelectedIndexChanged"/>

                    </td></tr>

                  </table>

                  <hr />

                </ItemTemplate>

            </asp:DataList>

        </div>

    </form>

</body>

</html>

 

1.9、以編程方式創建 UpdatePanel 控件

若要以編程方式向頁中添加 UpdatePanel 控件,請創建 UpdatePanel 控件的新實例。然後,通過使用 ContentTemplateContainer 屬性和 ControlCollection..::.Add 方法向新實例添加控件。不要直接向 ContentTemplate 屬性添加控件。

當以編程方式添加 UpdatePanel 控件時,僅來自與 UpdatePanel 控件處於同一命名容器中的控件的回發可用作面板的觸發器。

下面的示例演示如何以編程方式向頁中添加 UpdatePanel 控件。此示例通過使用 ContentTemplateContainer 屬性向面板添加 Label Button 控件。因爲 ChildrenAsTriggers 屬性默認爲 true,所以 Button 控件用作面板的觸發器。

<%@ Page Language="C#" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

 

    protected void Page_Load(object sender, EventArgs e)

    {

        UpdatePanel up1 = new UpdatePanel();

        up1.ID = "UpdatePanel1";

        up1.UpdateMode = UpdatePanelUpdateMode.Conditional;

        Button button1 = new Button();

        button1.ID = "Button1";

        button1.Text = "Submit";

        button1.Click += new EventHandler(Button_Click);

        Label label1 = new Label();

        label1.ID = "Label1";

        label1.Text = "A full page postback occurred.";

        up1.ContentTemplateContainer.Controls.Add(button1);

        up1.ContentTemplateContainer.Controls.Add(label1);

        Page.Form.Controls.Add(up1);

 

    }

    protected void Button_Click(object sender, EventArgs e)

    {

        ((Label)Page.FindControl("Label1")).Text = "Panel refreshed at " +

            DateTime.Now.ToString();

    }

 

</script>

 

<html >

<head id="Head1" runat="server">

    <title>UpdatePanel Added Programmatically Example</title>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <asp:ScriptManager ID="TheScriptManager"

                               runat="server" />

        </div>

    </form>

</body>

</html>

 

1.10、與 UpdatePanel 控件不兼容的控件

下面的 ASP.NET 控件與部分頁更新不兼容,因此不應在 UpdatePanel 控件內使用:

·處於多種情況下的 TreeView 控件。一種情況是啓用了不是異步回發的一部分的回調。另一種情況是將樣式直接設置爲控件屬性,而不是通過使用對 CSS 樣式的引用來隱式設置控件的樣式。還有一種情況是 EnableClientScript 屬性爲 false(默認值爲 true)。另外,還有一種情況是在異步回發之間更改了 EnableClientScript 屬性的值。 ·Menu 控件(將樣式直接設置爲控件屬性,而不是通過使用對 CSS 樣式的引用來隱式設置控件的樣式時)。

·FileUpload HtmlInputFile 控件(當它們作爲異步回發的一部分用於上載文件時)。

·GridView DetailsView 控件(當它們的 EnableSortingAndPagingCallbacks 屬性設置爲 true 時)。默認值爲 false

·LoginPasswordRecoveryChangePassword CreateUserWizard 控件(其內容尚未轉換爲可編輯的模板)。

·Substitution 控件。

 

若要在 UpdatePanel 控件內使用 FileUpload HtmlInputFile 控件,請將提交文件的回發控件設置爲面板的 PostBackTrigger 控件。僅可以在回發方案中使用 FileUpload HtmlInputFile 控件。

所有其他控件都可以在 UpdatePanel 控件內發揮作用。不過,在某些情況下,控件在 UpdatePanel 控件內可能不會按預期方式工作。這些情況包括:

·通過調用 ClientScriptManager 控件的註冊方法註冊腳本。

·在該控件呈現過程中直接呈現腳本或標記,例如,通過調用 Write 方法。

 

如果該控件調用 ClientScriptManager 控件的腳本註冊方法,則也許能夠改用 ScriptManager 控件相應的腳本註冊方法。在此情況下,該控件可以在 UpdatePanel 控件內工作。

 

1.11、在 UpdatePanel 控件內使用 Web 部件控件

ASP.NET Web 部件是一組集成控件,用於創建網站使最終用戶可以直接從瀏覽器修改網頁的內容、外觀和行爲。可以在 UpdatePanel 控件內使用 Web 部件控件,但具有以下限制:

·每個 WebPartZone 控件都必須在同一 UpdatePanel 控件內。例如,頁上不能具有兩個帶有各自的 WebPartZone 控件的 UpdatePanel 控件。

·WebPartManager 控件管理 Web 部件控件的所有客戶端狀態信息。它必須在頁面上的最外部的 UpdatePanel 控件內。

·不能通過使用異步回發導入或導出 Web 部件控件。(執行此任務需要 FileUpload 控件,該控件不能與異步回發一起使用。)默認情況下,導入 Web 部件控件將執行完全回發。

·在異步回發過程中,不能添加或修改 Web 部件控件的樣式。

 

1.12、異步回發過程中不支持的屬性和方法

在異步回發過程中,不支持在頁面上設置默認回發按鈕的以下方案:

·在異步回發過程中以編程方式更改 DefaultButton

·在異步回發過程中,當 Panel 控件不在 UpdatePanel 控件內部時以編程方式更改 DefaultButton

 

僅在回發方案(不包括異步回發方案)下才支持 HttpResponse 類的以下方法:

BinaryWriteClearClearContentClearHeadersCloseEndFlush

TransmitFileWriteWriteFileWriteSubstitution

發佈了247 篇原創文章 · 獲贊 15 · 訪問量 51萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章