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

时间:2013-02-04 05:59:33

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

我正在设置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>

2 个答案:

答案 0 :(得分: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”

答案 1 :(得分:0)

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

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

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