TheNawaKer TheNawaKer - 1 year ago 86
ASP.NET (C#) Question

User Control, Delegate and UpdatePanel Issue

I am currently running up against a problem with the UpdatePanel. One of my pages has two User Controls I'll call A and B.

The User Control A has an UpdatePanel containing several ASP TextBox.

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

The User Control B has an UpdatePanel containing a Panel and an AjaxModalPopup linked to it. In the Panel, there is a Repeater and an ASP button.

<asp:UpdatePanel runat="server" ID="upB" UpdateMode="Conditional">
<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">
<asp:Panel runat="server" ID="pnlHeader" class="header">
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:global, AttentionPopup %>"></asp:Label>
<asp:Repeater runat="server" ID="rptPopup" OnItemDataBound="rptPopup_ItemDataBound">
<-- Content -->
<div class="or-spacer">
<div class="mask"></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" />
<ajax:ModalPopupExtender ID="popupContainer" runat="server" BackgroundCssClass="modalBackground" PopupControlID="pnlPopupContainer" TargetControlID="btnActivePopupContainer" Drag="true" PopupDragHandleControlID="pnlHeader"></ajax:ModalPopupExtender>

When the user clicks the OK button of the UserControl B, it triggers a delegate that calls a method in the UserControl A that performs updating of some fields and the update method of the UpdatePanel "upA".

The delegate:

//Delegate Call When the user clicks the ok Button
popup.okHandler = (x) =>

in the UserControl A's code behind:

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

When the method upOpticien.Update() is called, I get this error:

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

I don't understand why I get this error. I tried to set the UpdateMode of the UpdatePanel to "Always", the application no longer crashes but the fields are not updated.

ideas ?

Answer Source

I finally found how to fix my problem. Instead of calling the delegate directly, I must use the InvokeMember function so that the delegate is executed correctly and can edit the various fields and update the UpdatePanel

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

(here, OkHandler is my delegate)

However, note that using an anonymous method will not work, it's imperative that the delegate points to a method with a name for InvokeMember works.