如何阻止 UpdatePanel 导致整个页面回发? [英] How to stop UpdatePanel from causing whole page postback?

查看:31
本文介绍了如何阻止 UpdatePanel 导致整个页面回发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET 3.5 并在 Community Server 2008 框架内构建页面.

I am using .NET 3.5 and building pages inside of the Community Server 2008 framework.

在其中一个页面上,我试图让 UpdatePanel 正常工作.

On one of the pages, I am trying to get an UpdatePanel working.

我直接从 ASP.NET 网站获取了一个示例,通过单击按钮将 UpdatePanel 中的时间更新为当前时间,但是由于某种原因,当我尝试执行该功能时,整个页面都会刷新.

I took a sample straight from ASP.NET website, update a time in an UpdatePanel to current time by clicking a button, but for some reason when I try and perform the function the whole page refreshes.

这是我所拥有的:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();
    Label2.Text = "Panel refreshed at " + DateTime.Now.ToString();
}

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

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <fieldset>
            <legend>UpdatePanel</legend>
            <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br />
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>

每当我点击按钮时,面板都会更新 - 但整个页面都会回传!我可以看到整个页面都在闪烁.我到底做错了什么?

Whenever I click the button, sure the panel updates - but the whole page posts back! I can see the whole page flashing. What the heck am I doing wrong?

我在嵌套的母版页中,但我不确定这是否有问题.我正在使用的这个社区服务器框架中是否有某些东西会导致所有事件回发?

I am inside of a nested Masterpage, but I'm not sure if this is a problem. Could there be something in this Community Server framework that I'm using that causes all events to postback?

推荐答案

您是否尝试在触发器部分将 Button1 设置为 AsyncPostBackTrigger ?将 ChildrenAsTriggers 属性设置为 true,将 UpdateMode 属性设置为 Conditional.

Did you try setting Button1 as an AsyncPostBackTrigger in the Triggers section? Set the ChildrenAsTriggers property to true and the UpdateMode property to Conditional.

protected void Button1_Click(object sender, EventArgs e)
{    
    Label1.Text = "Panel refreshed at " + DateTime.Now.ToString();    
    UpdatePanel1.Update();
}    

<asp:ScriptManager ID="ScriptManager1" runat="server"/>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
    <Triggers>        
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />    
    </Triggers>    
    <ContentTemplate>        
        <fieldset>            
            <legend>UpdatePanel</legend>            
            <asp:Label ID="Label1" runat="server" Text="Panel created."></asp:Label><br />            
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />        
        </fieldset>    
    </ContentTemplate>
</asp:UpdatePanel>

这篇关于如何阻止 UpdatePanel 导致整个页面回发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆