如何让UpdatePanel在自定义事件后进行更新?

问题描述 投票:1回答:2

我正在设置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>
asp.net ajax asp.net-ajax updatepanel
2个回答
0
投票

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

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

你应该在你的标记中设置UpdateMode="Conditional"


0
投票
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属性设置为“Always”

© www.soinside.com 2019 - 2024. All rights reserved.