如何阻止UpdatePanel导致整个页面回发?

发布于 2024-07-16 11:06:46 字数 1157 浏览 6 评论 0原文

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

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

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

这就是我所拥有的:

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>

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

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

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

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

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.

Here is what I have:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

感悟人生的甜 2024-07-23 11:06:46

您是否尝试在“触发器”部分中将 Button1 设置为 AsyncPostBackTrigger ? 将 ChildrenAsTriggers 属性设置为 true,将 UpdateMode 属性设置为 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>

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>
冰雪之触 2024-07-23 11:06:46

我在上面的代码示例中没有看到 Label2 。 如果 Label2 位于 UpdatePanel 外部,则会发生全页刷新,因为这是页面正确更新 Label2 所需要的。

默认情况下,UpdatePanel 只会在被其中的控件触发时动态刷新其中的内容。 如果您需要进行一些更高级的更新,例如面板外部的按钮导致刷新或更新不同面板中的标签,那么您需要在 UpdatePanel 上设置 Conditional 属性并进行一些手动 Update 调用在你的代码中。

I'm not seeing Label2 in your code sample above. If Label2 is located outside the UpdatePanel, a full page refresh will occur because that is what is required for the page to properly update Label2.

By default, UpdatePanels will only dynamically refresh the content within them, when triggered by controls within them. If you need to do some fancier updates, say a button outside of the panel causing the refresh or a label in a different panel to be updated, then you need to set the Conditional attribute on your UpdatePanel(s) and make some manual Update calls in your code.

难忘№最初的完美 2024-07-23 11:06:46

另一个可能的原因是,如果页面具有 ClientIDMode="static",那么您希望仅刷新 UpdatePanel 的控件将刷新整个页面。

要解决此问题,您只需在控件上设置 ClientIDMode="AutoID" 即可触发 UpdatePanel 回发。

Another possible reason is that if the page has ClientIDMode="static", then controls that you expect to refresh just the UpdatePanel will refresh the whole page.

To fix the problem, you just need to set ClientIDMode="AutoID" on the control(s) which should trigger the UpdatePanel post back.

抱着落日 2024-07-23 11:06:46

如果您有一个从 .net Framework v1.1 升级的旧项目,请从您的 Web 配置中删除此行以使其正常工作:

<xhtmlConformance mode="Legacy"/>

该标志由升级过程添加,并防止部分页面回发。

If you have an old project that was upgraded from .net framework v1.1, then remove this line from your web config to make it work:

<xhtmlConformance mode="Legacy"/>

That flag is added by the upgrade process and prevents partial page postbacks.

柠栀 2024-07-23 11:06:46

在 UpdatePanel 控件上设置 ChildrenAsTriggers="true"

Set ChildrenAsTriggers="true" on your UpdatePanel control.

魂归处 2024-07-23 11:06:46

“默认情况下,UpdatePanel 只会在被其中的控件触发时动态刷新其中的内容。”

否则整个页面都会刷新! 这才是重点!

"By default, UpdatePanels will only dynamically refresh the content within them, when triggered by controls within them."

Otherwise the whole page will be refreshed! Thats the point!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文