【问题标题】:User Control, Delegate and UpdatePanel Issue用户控制、委托和更新面板问题
【发布时间】:2016-11-26 15:25:56
【问题描述】:

我目前遇到了 UpdatePanel 的问题。我的一个页面有两个用户控件,我将分别称为 A 和 B。

用户控件 A 有一个包含多个 ASP 文本框的 UpdatePanel。

<asp:UpdatePanel ID="upA" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true" style="position: relative;">
    <ContentTemplate>
      <-- content here -->
    </ContentTemplate>
</asp:UpdatePanel>

用户控件 B 有一个 UpdatePanel,其中包含一个 Panel 和一个链接到它的 AjaxModalPopup。在面板中,有一个中继器和一个 ASP 按钮。

<asp:UpdatePanel runat="server" ID="upB" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Button ID="btnActivePopupContainer" runat="server" Style="display: none;" />
    <asp:Panel ID="pnlPopupContainer" runat="server" CssClass="modalPopup modalPopupDraggable" Style="display: none;" Width="750px">
        <asp:UpdatePanel ID="upPopup" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Panel runat="server" ID="pnlHeader" class="header">
                    <asp:Label ID="Label1" runat="server" Text="<%$ Resources:global, AttentionPopup %>"></asp:Label>
                </asp:Panel>
                <article>
                    <asp:Repeater runat="server" ID="rptPopup" OnItemDataBound="rptPopup_ItemDataBound">
                        <ItemTemplate>
                           <-- Content -->
                       </ItemTemplate>
                    </asp:Repeater>
                </article>
                <footer>
                    <div class="or-spacer">
                        <div class="mask"></div>
                    </div>
                    <div class="footer">
                        <asp:Panel runat="server" ID="pnlBtnBug">
                            <asp:Button ID="btnOK" runat="server" CssClass="btn yes" Text="<%$ Resources:global, Ok %>" OnClick="btnOK_Click" />
                        </asp:Panel>
                    </div>
                </footer>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>
    <ajax:ModalPopupExtender ID="popupContainer" runat="server" BackgroundCssClass="modalBackground" PopupControlID="pnlPopupContainer" TargetControlID="btnActivePopupContainer" Drag="true" PopupDragHandleControlID="pnlHeader"></ajax:ModalPopupExtender>
</ContentTemplate>

当用户单击 UserControl B 的 OK 按钮时,它会触发一个委托,该委托调用 UserControl A 中执行某些字段更新的方法和 UpdatePanel "upA" 的更新方法。

委托人:

//Delegate Call When the user clicks the ok Button
popup.okHandler = (x) =>
{
   A.ChooseOpticienCode(x.Value);
};

在 UserControl A 的代码后面:

public void ChooseOpticienCode(string code)
{
    <-- some treatments -- >
    upOpticien.Update(); <-- crash here
}

当方法 upOpticien.Update() 被调用时,我得到这个错误:

System.InvalidOperationException: The Update method can only be called on UpdatePanel with ID 'upOpticien before Render.

我不明白为什么会出现此错误。我尝试将 UpdatePanel 的 UpdateMode 设置为“Always”,应用程序不再崩溃但字段未更新。

想法?

【问题讨论】:

  • 您为什么尝试手动更新 UpdatePanel 而不是为其设置触发器?无论如何,看起来错误告诉您无法在页面生命周期的那部分运行 .Update() 方法。您应该尝试查找/阅读有关该方法的文档。
  • 我尝试手动更新它,因为它在后面的代码中我更改了 UpdatePanel 包含的所有文本框的值。因此,由于在代码隐藏中修改了值,因此不会启动任何事件,因此这里不需要触发器。

标签: c# asp.net user-controls delegates


【解决方案1】:

我终于找到了解决问题的方法。我必须使用 InvokeMember 函数而不是直接调用委托,以便正确执行委托并可以编辑各种字段并更新 UpdatePanel

 page.GetType().InvokeMember(OkHandler.Method.Name, System.Reflection.BindingFlags.InvokeMethod, null, page, new[] { this });

(这里,OkHandler 是我的代表)

但是,请注意,使用匿名方法是行不通的,委托必须指向一个名称为 InvokeMember 的方法。

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 2020-10-19
    相关资源
    最近更新 更多