2013-05-27 234 views
1

我目前在更新面板中有更新面板。例如,我有一個主頁面,主頁面內的所有內容都在一個updatepanel中,我將命名爲updatepanel1。位於使用母版頁的aspx頁面中的較小更新面板稱爲updatepanel2。更新面板中的更新面板

但是我有一個計時器對象,我只希望刷新updatepanel2。但是,我的整個頁面刷新,而不僅僅是updatepanel2。

究竟應該如何編寫代碼才能只刷新updatepanel2,而不是UpdatePanel1,因爲我不希望刷新母版頁中的全部內容。

編輯:看起來像它不會刷新的原因是因爲我在頁面中留下一個刷新HTML元標記,並忘記將其刪除。

+0

這兩個「UpdateMode」是什麼?你能提供你的代碼嗎? –

回答

1

母版

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %> 

<!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></title> 
    <asp:ContentPlaceHolder ID="head" runat="server"> 
    </asp:ContentPlaceHolder> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
      </asp:ScriptManager>    
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
      <ContentTemplate> <asp:Label Id="lblmaster" runat="server" Text="Label"></asp:Label> 
       <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> 
      </ContentTemplate>    
      </asp:UpdatePanel>  
    </div> 
    </form> 
</body> 
</html> 

aspx頁面ASPX頁面

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %> 
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> 
    <ContentTemplate> 
     <asp:Label ID="labelform" runat="server" Text="Label"></asp:Label> 
      <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="10"> 
    </asp:Timer> 
    </ContentTemplate> 
    <Triggers> 
    <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick"/> 
    </Triggers> 

    </asp:UpdatePanel> 

</asp:Content> 

代碼隱藏

using System; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace WebApplication1 
{ 
    public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void Timer1_Tick(object sender, EventArgs e) 
     { 
      labelform.Text = DateTime.Now.ToString(); 
     // UpdatePanel1.Update(); 
      Label lblmaster = this.Master.FindControl("lblmaster") as Label; 
      lblmaster.Text = DateTime.Now.ToString(); 
     } 
    } 
} 

注:

  1. 都更新面板的設置更新模式爲「有條件」

  2. 添加時間控制在updatePanel2

  3. 添加AsynhronousTrigger內UpadatePanel2並設置控件ID定時器和蜱eventas事件名稱。

  4. 將你的代碼添加到timer_tick中,在代碼後面翻轉並查看。

重要的一點是在這裏你必須做的非同步調用服務器和更新的updatepane爲有條件的頁面集更新模式只有特定的藥水,你必須設置一些條件在觸發

更新的特定部分

希望這可以幫到你

謝謝你繼續編碼.........! :)

0

documentation from Microsoft講述了導致UpdatePanel更新的不同情況。我強烈建議您將更新面板的使用限制爲僅需要它的控件,而不是將整個頁面包裝在其中。

或者,更好的辦法是消除UpdatePanel並使用JQuery或Microsoft Ajax來更新您的內容。它的副作用較少。