UpdatePanel 内的 ScriptControl
我有一个 ScriptControl
(需要 ScriptManager
),使用 JavaScript 来处理客户端交互,并使用 ICallbackEventHandler
来来回通信。一切都与页面上的一个或多个控件实例完美配合。我将控件放置在 GridView 中并进行排序,它仍然有效。但是,我将 GridView 放在 UpdatePanel 中,现在每当我排序时,每个实例都会出现以下错误:
Sys.InvalidOperationException:具有相同 id 的两个组件无法将“GridView_ctl02_MyControl”添加到应用程序中。
有人可以为我指出如何解决此问题的正确方向吗?我假设在尝试使用相同的 $create()
新对象之前,ScriptManager
不会处理旧的 Sys.UI.Control
对象ID。我认为 UpdatePanel
/ScriptManager
组合会自动处理将被替换的对象的处理?
编辑:此页面似乎支持我的想法:
使用RegisterDispose方法 注册控件的处置脚本 位于 UpdatePanel 内 控制。异步期间 回发,UpdatePanel 控件可以 更新、删除或创建。 当 面板被更新或删除,任何 处置已注册的脚本 对于内部的控件 UpdatePanel 被调用。在典型的 页面开发场景,你不 必须调用RegisterDispose 方法。
只是为了仔细检查,我在 JavaScript dispose()
函数中放置了 alert("dispose " + this.element.id)
。每个实例都会发出警报 dispose GridView_ctl02_MyControl
,但之后我收到错误消息,指出两个组件不能具有相同的名称 GridView_ctl02_MyControl
。我很茫然...
I have a ScriptControl
(requires ScriptManager
) with JavaScript to handle client-side interactions and ICallbackEventHandler
to communicate back and forth. Everything works perfectly with one or multiple instances of the control on a page. I placed the control inside a GridView
with sorting and it still works. However, I place the GridView
in an UpdatePanel
and now whenever I sort I get the following error for each instance:
Sys.InvalidOperationException: Two components with the same id 'GridView_ctl02_MyControl' can't be added to the application.
Can someone point me in the right direction on how to solve this? I am assuming ScriptManager
is not disposing of the old Sys.UI.Control
objects before trying to $create()
the new ones with the same ID. I thought the UpdatePanel
/ScriptManager
combination would automatically take care of disposing objects that would be replaced?
Edit: This page appears to support what I thought: http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.registerdispose.aspx
Use the RegisterDispose method to
register dispose scripts for controls
that are inside an UpdatePanel
control. During asynchronous
postbacks, UpdatePanel controls can be
updated, deleted, or created. When a
panel is updated or deleted, any
dispose scripts that are registered
for controls that are inside the
UpdatePanel are called. In typical
page development scenarios, you do not
have to call the RegisterDispose
method.
Just to double check I placed an alert("dispose " + this.element.id)
inside my JavaScript dispose()
function. Every single instance alerts dispose GridView_ctl02_MyControl
, but afterwards I get the error that two components can't have the same name GridView_ctl02_MyControl
. I'm at a loss...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当页面卸载时,我的组件的
dispose()
方法被调用,并且Sys.Application.removeComponent()
也被调用。当UpdatePanel
重新加载时,仅调用dispose()
方法。现在我已经通过将Sys.Application.removeComponent(this);
放入dispose()
中解决了这个问题。我没有找到诸如 $remove (类似于 $create)之类的快捷方式,这意味着您预计不会经常需要它。这似乎合乎逻辑,因为即使组件的相关 DOM 元素(如果有)已被 UpdatePanel 替换,您也可以保持组件加载。这样您就可以更好地控制组件的寿命。我无法想象一个用例,但我相信你可以想出一个。
如果我还有更好的方法,请告诉我!
When the page unloads, my component's
dispose()
method is called andSys.Application.removeComponent()
is also called. When theUpdatePanel
reloads, onlydispose()
method is called. For now I have solved this by puttingSys.Application.removeComponent(this);
inside thedispose()
. I didn't find a shortcut such as $remove (similar to $create), implying you aren't expected to need this often.This seems logical in that you can keep a component loaded even after its related DOM elements (if any) have been replaced by the UpdatePanel. This way you have more control over the component's life. I can't imagine a use case, but I'm sure you could come up with one.
If I am way off and there is a better approach, please let me know!