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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/728043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:45:03  来源:igfitidea点击:

How to stop UpdatePanel from causing whole page postback?

c#asp.net.net-3.5updatepanelcommunity-server

提问by user53885

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

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

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

在其中一个页面上,我试图让 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.

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

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?

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

采纳答案by Michael Kniskern

Did you try setting Button1as an AsyncPostBackTriggerin the Triggers section? Set the ChildrenAsTriggersproperty to trueand the UpdateModeproperty to Conditional.

您是否尝试在触发器部分设置Button1为 a 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>

回答by Dillie-O

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.

我在上面的代码示例中没有看到 Label2。如果 Label2 位于 UpdatePanel 之外,则会发生整页刷新,因为这是页面正确更新 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.

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

回答by Rex M

Set ChildrenAsTriggers="true"on your UpdatePanel control.

ChildrenAsTriggers="true"在您的 UpdatePanel 控件上设置。

回答by Gerhard Meyer

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

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

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

否则整个页面将被刷新!这才是重点!

回答by MikeBeaton

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.

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

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

要解决此问题,您只需要设置ClientIDMode="AutoID"应该触发 UpdatePanel 回发的控件。

回答by Alan

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:

如果你有一个从 .net framework v1.1 升级的旧项目,那么从你的 web 配置中删除这一行以使其工作:

<xhtmlConformance mode="Legacy"/>