【问题标题】:How to make UpdatePanel to do Update after custom Event?如何让 UpdatePanel 在自定义事件后进行更新?
【发布时间】:2013-02-04 05:59:33
【问题描述】:

我正在设置 UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional; 进行手动更新,但它不适用于某些自定义事件,当我在这里有一些类似的事件时:

protected void Button1_Click(object sender, EventArgs e) {
    discovery.FindAlreadyRegisteredServices();
    discovery.discoveryClient.FindCompleted += FoundEvent;

protected void FoundEvent(object sender, FindCompletedEventArgs e) {
    Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
    UpdatePanel1.Update();
    }

我的项目失败了:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Internals.dll

Additional information: The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.

即使我设置ChildrenAsTriggers 与否。错误消息对我来说不清楚,我不明白在处理我的事件后我应该如何处理更新?

补充:

aspx:

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <ContentTemplate>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <asp:ListView ID="ListView1" runat="server">
        </asp:ListView>
    </ContentTemplate>
</asp:UpdatePanel>

【问题讨论】:

  • 当 button1 被点击时你得到这个异常?
  • 请分享aspx代码。
  • 我在 UpdatePanel1.Update() 上遇到异常;

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


【解决方案1】:

我想你应该像这样改变你的标记

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    ....

您应该在自己的标记中设置UpdateMode="Conditional"

【讨论】:

    【解决方案2】:
    protected void Button1_Click(object sender, EventArgs e) {
        discovery.FindAlreadyRegisteredServices();
        discovery.discoveryClient.FindCompleted += FoundEvent;
      // Try to call the update method after calling the custom even but in the click event of the button. Ensure you update the Trigger accordingly in the update panel
     **UpdatePanel1.Update();**
    }
    
    protected void FoundEvent(object sender, FindCompletedEventArgs e) {
        Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
           }
    

    尝试将AsyncPostBackTrigger添加到更新面板,更新模式为条件

    虽然你明确地做同样的事情。

    <Triggers>  
                    <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />  
                </Triggers>  
    

    只是为了检查是否有其他问题,您可以将更新面板的 updateMode 属性设置为“始终”

    【讨论】:

    • 我曾经为按钮设置 ChildrenAsTriggers,但我不需要按钮,我需要我的自定义事件。
    • 将您的标签放入更新面板中,更新模式为 "always",然后在您的客户事件中调用更新面板的 update 方法.无需使用 ChildrenAsTriggers 属性。它仅在 UpdateMode 属性设置为 Conditional 时使用。如果这可行,您可以稍后出于性能原因将 UpdateMode 更改为有条件的。
    • 只有当 UpdateMode 设置为 Conditional 时,才能在 ID 为“UpdatePanel1”的 UpdatePanel 上调用 Update 方法。
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2023-03-30
    相关资源
    最近更新 更多