2010-04-29 62 views
0

我想將UpdatePanel的觸發器設置爲同一頁面上的UpdatePanel外部的用戶控件。用戶控件在設計時添加,而不是在運行時。
如果我靜態聲明觸發器,我得到一個錯誤「無法在UpdatePanel」中找到觸發器的ID爲'xx'的控件。我嘗試在Page_Init或Page_Load中的運行時添加觸發器,但它失敗,用戶控件爲空,儘管它啓用了ViewState。有人有關於如何解決這個問題的想法?爲什麼我的用戶控件在回發中沒有實例化?

下面是用戶控件的代碼:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ComponentDropDownControl.ascx.cs" Inherits="ComponentDropDownControl" EnableViewState="true" %> 
<asp:DropDownList ID="ComponentDropDown" runat="server" DataSourceID="ComponentFile" 
DataTextField="name" DataValueField="name" OnSelectedIndexChanged="ComponentDropDown_SelectedIndexChanged" AutoPostBack="True" EnableTheming="True"> 
</asp:DropDownList><asp:XmlDataSource ID="ComponentFile" runat="server" DataFile="~/App_Data/Components.xml" XPath="//component"></asp:XmlDataSource> 

這裏,它是在aspx頁面:

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="create.aspx.cs" Inherits="create" Title="Create task" %> 

<%@ Register Src="ComponentDropDownControl.ascx" TagName="ComponentDropDownControl" 
TagPrefix="uc1" %> 
... 
<uc1:ComponentDropDownControl ID="CustomComponentDropDown" runat="server" EnableViewState="true" /> 

在aspx頁面的Page_Load中的功能,下面幾行工作第一次,但第一次PostBack失敗(第2行,CustomComponentDropDown爲空)。

AsyncPostBackTrigger trigger = new AsyncPostBackTrigger(); 
trigger.ControlID = CustomComponentDropDown.UniqueID.ToString(); 
UpdatePanel1.Triggers.Add(trigger); 

編輯:用戶控件不實例化,因爲它緩存。事實上,用戶控件被緩存並能夠處理事件似乎是不可能的。我必須錯過一些東西,因爲這對我來說似乎很糟糕。
如果有人知道我做錯了什麼,請告訴。

回答

0

在什麼情況下你想處理事件並控制緩存?我看到處理事件的唯一原因是要麼改變內部狀態,要麼改變顯示狀態。無論哪種情況,緩存都會消除這種可能性。

+0

用戶控件包含一個大的下拉(5K元素)不改變,因此高速緩存。但是,當用戶選擇一個項目時,必須觸發一些查詢來更新其他字段(所提及的UpdatePanel中的字段)。那是錯的嗎? – Antoine 2010-04-29 15:59:59

0

您是否將控件註冊爲異步回發控件?

ScriptManager1.RegisterAsyncPostBackControl(CustomComponentDropDown);

此外,不應該

trigger.ControlID = CustomComponentDropDown.UniqueID.ToString(); 

簡單地

trigger.ControlID = CustomComponentDropDown.ID; 
+0

這不會使CustomComponentDropDown返回,它在第一個回發的Page_Load上時仍爲null。 – Antoine 2010-04-29 16:53:17

相關問題