asp.net ajax技巧7

來源:互聯網
上載者:User
  繼續讀老章的書,這次是看到如何在一個master頁面中放些按鈕,然後點的時候,更新content頁面中的東西,而且可以做到局部更新。先看主MASTER頁面
master頁面的前台
<form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:Panel ID="MasterPanel1" runat="server" GroupingText="主控頁面">
               <asp:Button ID="MasterButton1" runat="server" Text="整頁更新" />
               <asp:Button ID="MasterButton2" runat="server" Text="局部更新" OnClick="MasterButton2_Click" Width="128px" />
            </asp:Panel>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
master頁面的後台

 protected void Page_Load(object sender, EventArgs e)
    {
        ScriptManager1.RegisterAsyncPostBackControl(MasterButton2);
    }

    public DateTime LastUpdate
    {
        get
        {
            if(ViewState["LastUpdate"] == null)
            {
                return DateTime.Now;
            }
            else
            {
                return Convert.ToDateTime(ViewState["LastUpdate"]);
            }
        }
        set
        {
            ViewState["LastUpdate"] = value;
        }
    }

    protected void MasterButton2_Click(object sender, EventArgs e)
    {
        LastUpdate = DateTime.Now;
        UpdatePanel up1 = (UpdatePanel)(ContentPlaceHolder1.FindControl("UpdatePanel1"));
        up1.Update();
    }

注意這裡因為希望首頁面的“局部更新”按鈕能引發非同步更新內容頁面中的UPDATEPANEL控制項的內容,因此必須用
        ScriptManager1.RegisterAsyncPostBackControl(MasterButton2);
註冊該按鈕引發的事件。
   要注意masterbutton2_click事件的寫法,調用內容頁中的updatepanel控制項的UPDATE方法來更新內容。
然後在內容頁中前台中

<%@ Page Language="C#" MasterPageFile="~/ThirdMasterPage.master" AutoEventWireup="true"
    CodeFile="CH3_DemoForm040.aspx.cs" Inherits="CH3_DemoForm040" Title="如何於主控頁面中使用 UpdatePanel 控制項" %>

<%@ MasterType VirtualPath="ThirdMasterPage.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Panel ID="Panel2" GroupingText="內容頁面" runat="server">
        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
            <ContentTemplate>
                <p>
                    上一次更新:<strong><%= Master.LastUpdate.ToString() %></strong></p>
                <asp:Button ID="ContentButton" OnClick="ContentButton_Click" runat="server" Text="局部更新">
                </asp:Button>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>
</asp:Content>

後台中,為其“局部更新”編寫相關的事件,以將當前日期時間給主控頁面的lastupdate屬性
protected void ContentButton_Click(object sender, EventArgs e)
    {
        Master.LastUpdate = DateTime.Now;
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.