我想将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);
编辑:用户控件不实例化,因为它缓存。事实上,用户控件被缓存并能够处理事件似乎是不可能的。我必须错过一些东西,因为这对我来说似乎很糟糕。
如果有人知道我做错了什么,请告诉。
用户控件包含一个大的下拉(5K元素)不改变,因此高速缓存。但是,当用户选择一个项目时,必须触发一些查询来更新其他字段(所提及的UpdatePanel中的字段)。那是错的吗? – Antoine 2010-04-29 15:59:59