【问题标题】:Updatepanel within an Update panel更新面板中的更新面板
【发布时间】:2013-05-27 02:10:06
【问题描述】:

我目前在更新面板中有一个更新面板。例如,我有一个母版页,母版页中的所有内容都在一个更新面板中,我将其命名为 updatepanel1。位于使用母版页的 aspx 页面中的较小更新面板称为 updatepanel2。

但是我有一个计时器对象,我只希望刷新 updatepanel2。但是,我的整个页面都会刷新,而不仅仅是 updatepanel2。

我应该如何编写代码以便只刷新 updatepanel2 而不是 Updatepanel1,因为我不希望刷新母版页中的全部内容。

编辑:似乎它不会刷新的原因是因为我在我的页面中留下了一个刷新 HTML 元标记而忘记删除它。

【问题讨论】:

  • 这两个的UpdateMode 是什么?你能提供你的代码吗?

标签: c# asp.net asp.net-ajax updatepanel


【解决方案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 页面

<%@ 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>

ASPX 页面的代码隐藏

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. 在 UpadatePanel2 中添加 AsynhronousTrigger 并将控件 ID 设置为计时器并将事件标记为 EventName。

  4. 将你的代码添加到timer_tick evert in code behind 中看看。

重要的一点是,您必须对服务器进行异步调用,并且仅将更新窗格的页面集更新模式的特定部分更新为条件,并且您必须设置一些条件来更新触发器中的特定部分

希望对你有帮助

谢谢你继续编码............! :)

【讨论】:

    【解决方案2】:

    documentation from Microsoft 讲述了导致 UpdatePanel 更新的不同情况。我强烈建议您将更新面板的使用限制为仅需要它的控件,而不是将整个页面包装在其中。

    或者,更好的是,消除 UpdatePanel 并使用 JQuery 或 Microsoft Ajax 来更新您的内容。它具有较少的困难副作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多