为什么我的用户控件没有在回发时实例化?

发布于 2024-08-30 23:02:16 字数 1677 浏览 1 评论 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 为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沩ん囻菔务 2024-09-06 23:02:16

在什么情况下您希望处理事件并缓存控件?我认为处理事件的唯一原因是更改内部状态或更改显示状态。无论哪种情况,缓存都会消除这种可能性。

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.

我要还你自由 2024-09-06 23:02:16

您是否将该控件注册为异步回发控件?

ScriptManager1.RegisterAsyncPostBackControl(CustomComponentDropDown);

另外,不应该

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

简单地是

trigger.ControlID = CustomComponentDropDown.ID;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文