为什么我的用户控件没有在回发时实例化?
我想将 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 为 null)。
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = CustomComponentDropDown.UniqueID.ToString();
UpdatePanel1.Triggers.Add(trigger);
编辑:用户控件未实例化,因为它已被缓存。事实上,让用户控件既被缓存又能够处理事件似乎是不可能的。我一定错过了一些东西,因为这对我来说似乎是非常糟糕的行为。
如果有人知道我做错了什么,请告诉我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在什么情况下您希望处理事件并缓存控件?我认为处理事件的唯一原因是更改内部状态或更改显示状态。无论哪种情况,缓存都会消除这种可能性。
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.
您是否将该控件注册为异步回发控件?
ScriptManager1.RegisterAsyncPostBackControl(CustomComponentDropDown);
另外,不应该
简单地是
Did you register the Control as a async postback control?
ScriptManager1.RegisterAsyncPostBackControl(CustomComponentDropDown);
Also, shouldn't
Simply be