我目前在更新面板中有一个更新面板。例如,我有一个母版页,母版页中的所有内容都在updatepanel中,我将其命名为updatepanel1。使用母版页的aspx页中的较小更新面板称为updatepanel2。
但是,我有一个timer对象,我只希望刷新updatepanel2。但是,我的整个页面都会刷新,而不仅仅是updatepanel2。
由于我不希望刷新母版页中的整个内容,我到底应该如何编写代码,使其只刷新updatepanel2而不刷新Updatepanel1。
编辑:它不会刷新的原因似乎是因为我在页面中留下了一个刷新HTML元标签,并且忘记了删除它。
发布于 2013-05-27 13:39:22
MasterPage
<%@ 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();
}
}
}备注:
在updatePanel2
重要之处在于,您必须对服务器进行异步调用,以便仅更新页面的特定部分,并将updatepane的更新模式设置为有条件的,您还必须设置一些条件,以便在触发器中更新特定部分
希望这能对你有所帮助
感谢您继续编码......!:)
发布于 2013-05-27 11:22:58
documentation from Microsoft讲述了导致UpdatePanel更新的不同情况。我强烈建议您将更新面板的使用限制为只使用需要它的控件,而不是将整个页面包装在其中。
或者,更好的做法是去掉UpdatePanels,使用JQuery或Microsoft Ajax来更新内容。它有较少的困难副作用。
https://stackoverflow.com/questions/16765519
复制相似问题