具有相同 ID 的多个控件“add0”; 被发现。 FindControl 要求控件具有唯一 ID

发布于 2024-07-16 10:13:32 字数 858 浏览 5 评论 0原文

以前称为:如何在aspx中加载时处理动态创建的控件,

以回答以下问题:确定要恢复哪些控件所需的信息包含在专用的视图状态对象中。

我在代码隐藏页面中动态创建控件 - 这些控件都连接到单击处理程序,因此当发生回发时,我必须重新创建前一组控件,然后清除控件并根据上一次点击。

在正常情况下,这是编码并正常工作的,基本上如下:

in Page_Load
if not postback generate default buttons 
else if postback re-generate buttons that were shown on last page

in click_handler
Clear the dynamically generated buttons created in the Page_Load
generate new buttons based on the specific click being handled

但是,当服务器承受负载时,我们开始遇到问题:

每秒 5 个用户,我们开始遇到异常: 发现多个具有相同 ID“add0”的控件。 FindControl 要求控件具有唯一的 ID。

每秒有 100 个用户,我们开始遇到异常: 在 DataBind、Init、Load、PreRender 或 Unload 阶段无法修改控件集合。

一旦发生这种情况,所有后续请求都会出现相同的错误,并且必须重新启动 IIS。

可能是什么原因造成的?我该如何避免? html 请求在加载时是否可能会相互覆盖和干扰? 页面卸载后,对象是否会以某种方式进行传递,从而允许下一页加载绊倒它们?

Previously Called: How to deal with dynamically created controls under load in aspx

in response to a question below: the information required to determine which controls to restore is contained in a dedicated viewstate object.

I am dynamically creating controls in the codebehind page - these controls are all hooked up to click handlers so when a postback occurs I must re-create the previous set of controls, then clear the controls down and generate the new set of controls based on the previous click.

This is coded and working correctly under normal circumstances esentially as follows:

in Page_Load
if not postback generate default buttons 
else if postback re-generate buttons that were shown on last page

in click_handler
Clear the dynamically generated buttons created in the Page_Load
generate new buttons based on the specific click being handled

however when the server comes under load we start getting problems:

With 5 users per second we start getting the exception:
Multiple controls with the same ID 'add0' were found. FindControl requires that controls have unique IDs.

With 100 users per second we start getting the exception:
The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

Once this occurs all subsequent requests get the same error and IIS has to be re-started.

What could be causeing this and how can I avoid it? Do html requests possibly overwrite and interfere with each other when under load? do objects somehow hand around after a page unload in a manner that would allow the next page load to trip over them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

有深☉意 2024-07-23 10:13:32

您如何存储有关需要恢复的控件的信息? 如果您使用 ViewState 或 ControlState,那么我看不出负载会如何影响事物。 这就是任何复合控​​件的工作方式。

我想说的是,我在使用 Infragistics UltraWebGrid 时看到了您的第二个错误,但始终无法找到它。 从调用堆栈来看,EnsureChildControls 似乎是在加载阶段(或者可能是 LoadViewState)被调用。

How are you storing information about the controls you need to restore? If you are using ViewState or ControlState, then I don't see how load could affect things. That's how any of the composite controls do things.

I will say that I saw your second error while using the Infragistics UltraWebGrid, and never was able to track it down. From the call stack, it appeared that EnsureChildControls was being called during the Load phase (or maybe LoadViewState).

残疾 2024-07-23 10:13:32

使用私有静态变量来存储名称和表格单元格的字典,以便在页面生命周期期间不会重新创建表格单元格。

关键点是它被标记为静态 - 它应该是一个实例变量 - 最终结果是在负载下,当请求开始备份时,多个请求正在共享这个静态字典。

究竟发生了什么,我不是 100% 确定 - 但在中等负载下 FindControl 会找到多个同名的控件,在非常高的负载下,似乎一个请求会尝试修改一个控件(可能添加到它),而它处于一个来自其他请求的无效状态。

最终结果 - 如果您真的不知道自己在做什么 - 更喜欢实例变量而不是静态变量。

A private static variable was being used to store a dictionary of names and table cells so that table cells would not get re-created during the page lifecycle.

The key point is that it was marked static - it should have been an instance variable - the end result being that under load when requests started backing up then multpile requests were sharing this static dictionary.

exactly what happened i'm not 100% sure - but under medium loads FindControl would find multiple controls of the same name, under very high loads it seems one request would try to modify a control (probably add to it) while it was in an invalid state from the other request.

End result - if you dont really know what your doing - prefer instance variable sto static variables.

感情洁癖 2024-07-23 10:13:32

你所写的一切似乎都是正确且可行的。 这很可能是您的控件生成代码的问题。 也许如果您发布其中一些内容,我们可以更好地找到解决方案。

Everything you have written seems to be correct and doable. Most likely this is an issue with your control generation code. Perhaps if you post some of that we can better find a solution.

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