Asp.net
通過不同 ContentPlaceHolder 中的控制項觸發 UpdatePanel 的更新
我有一個包含兩個 ContentPlaceHolders 的頁面。一個有一個 DropDown 和另一個帶有內容的 UpdatePanel。
當 DropDown 的 selectedItemChanged 事件位於不同的 ContentPlaceholders 中時,如何觸發對 UpdatePanel 的更新?
由於 UpdatePanel1 不知道 DropDown1,因此以下操作不起作用:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true"> <ContentTemplate> Some content that needs to be updated here... </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="DropDown1" EventName="SelectedIndexChanged" /> </Triggers> </asp:UpdatePanel>一種方法是創建一個 ajax 頁面方法,當選擇 DropDown 的項目時,該方法將由頁面上的 javascript 呼叫。然後在後面的程式碼中,在該頁面方法中,呼叫 UpdatePanel1.Update()。
有沒有更簡單的選擇?
來自http://msdn.microsoft.com/en-us/library/system.web.ui.asyncpostbacktrigger.aspx
AsyncPostBackTrigger 引用的控制項必須與作為觸發器的更新面板位於同一命名容器中。不支持基於其他命名容器中的控制項的觸發器。
解決方法是使用觸發器引用的控制項的 UniqueID。不幸的是,直到控制項被添加到其父級(並且其父級已添加到其父級,一直到控制項樹),UniqueID 才被限定。
在您後面的程式碼中,嘗試:
UpdatePanel1.Triggers.Add(new AsyncPostBackTrigger() { ControlID = DropDown1.UniqueID, EventName = "SelectedIndexChanged", // this may be optional });