2016-07-22 81 views
0

我目前正在運行UpdatePanel的問題。我的一個頁面有兩個用戶控件,我會打電話給A和B.用戶控制,委託和UpdatePanel問題

用戶控件A有一個包含幾個ASP TextBox的UpdatePanel。

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

用戶控件B具有包含面板和鏈接到它的AjaxModalPopup一個UpdatePanel。在面板中,有一個Repeater和一個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> 

當用戶點擊該用戶控件B的確定按鈕時,它觸發調用在用戶控件A中的方法,其執行某些字段的更新和在UpdatePanel「UPA的更新方法的委託」。

委託:

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

背後的用戶控件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設置爲「始終」,應用程序不再崩潰,但字段沒有更新。

想法?

+0

你爲什麼要手動更新UpdatePanel而不是爲它設置觸發器?無論如何,它看起來像錯誤告訴你,你不能在頁面生命週期的那一部分運行.Update()方法。您應該嘗試查找/閱讀關於該方法的文檔。 – Seano666

+0

我嘗試手動更新它,因爲它在後面的代碼中,我更改了UpdatePanel包含的所有文本框的值。 因此,由於在代碼隱藏中修改了值,因此不會啓動任何事件,因此觸發器在此處不再需要。 – TheNawaKer

回答

0

我終於找到了如何解決我的問題。而不是直接調用委託的,我必須使用,以便正確地執行委託InvokeMember功能,可以編輯各個領域和更新的UpdatePanel

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

(這裏,OkHandler是我的代表)

然而請注意,使用匿名方法將不起作用,因此委託人必須指向具有InvokeMember名稱的方法。