views:

44

answers:

2

Hi,

I'd like to set the trigger of an UpdatePanel to a user control outside the UpdatePanel on the same page . The user control is added at design time, not at runtime.
If I statically declare the trigger, I get an error "A control with ID 'xx' cannot be found for the trigger in UpdatePanel". I tried to add the trigger at runtime in Page_Init or Page_Load, but it fails with the user control being null, although it has ViewState enabled. Has someone an idea on how to solve this?

Here is the code of the user control:

<%@ 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>

And here it is in the aspx page:

<%@ 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" />

In the Page_Load function of the aspx page, the following lines work at first time, but fail on the first PostBack (line 2, CustomComponentDropDown is null).

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

EDIT: user control is not instanciated because it is cached. In fact, it seems impossible to have a user control being both cached and able to handle events. I must be missing something because that seems pretty shitty behavior to me.
If anyone knows what I'm'doing wrong, please tell.

A: 

In what scenario would you want to handle events and have the control cached? The only reason I see to handle events is either to change internal state, or change the display state. In either case, caching will remove that possibility.

tster
user control contains a big dropdown (5K elements) which do not change, hence the caching. But when the user selects an item, that must trigger some queries to update some other fields (the ones in the mentioned UpdatePanel). Is that wrong?
Antoine
A: 

Did you register the Control as a async postback control?

ScriptManager1.RegisterAsyncPostBackControl(CustomComponentDropDown);

Also, shouldn't

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

Simply be

trigger.ControlID = CustomComponentDropDown.ID;
Serapth
That doesn't bring back the CustomComponentDropDown, it's still null when on the Page_Load of the first PostBack.
Antoine