ASP.NET 控件和更新面板
我正在动态创建一系列嵌套在更新面板内的客户端组件控件。 我第一次创建控件时,一切都按预期工作,但是,当我在更新面板上触发更新并且它执行部分回发时,控件会返回几个 javascript 错误,描述控件如何在页面上注册。
我收到一系列错误,内容如下: “错误:Sys.InvalidOperationException:两个具有相同 id“master_ctl40_CCB_PALETTES”的组件无法添加到应用程序中”
有人有什么想法吗?
I am creating a series of client side component controls on the fly that are nested inside a update panel. The first time I create the controls, everything works as desired, however, when i trigger an update on the update panel and it does a partial postback the controls come back with several javascript errors describing how the control is already registered on the page.
I get a series of errors that say something about like:
"Error: Sys.InvalidOperationException: Two components with the same id "master_ctl40_CCB_PALETTES" can't be added to the application"
Any ideas anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这些技巧:
Try these tricks:
在哪种情况下您会将组件添加到更新面板? 即,您是否将它们放置在页面加载事件中而不进行回发检查,或者将它们放置在更新面板加载事件中? ETC...
In which event are you adding the components to the update panel? I.e. have you placed them inside the page load event without a postback check or have you placed them inside the update panel load event? etc...
看起来您的客户端对象被创建了不止一次。
如果您希望客户端控件在更新面板刷新时被替换,它们应该继承自 Sys.UI.Control,该控件在其构造函数中采用一个元素。 当该元素被更新面板替换时,客户端对象将被处置然后重新创建。 如果您当前在服务器端使用 ScriptComponentDescriptor 来定义客户端控件实例,则您需要切换到 ScriptControlDescriptor。
听起来,您的客户端对象只是从 Sys.Component 继承,该对象将一直保留到手动处置为止,这就是为什么您会收到有关多个组件具有相同 ID 的错误。
我建议不要在每次回发时都使用新的 ID - 这只会不断创建新的客户端对象,而不会清理旧的对象。
Looks like your client object is being created more than once.
If you want your client side controls to be replaced when the update panel they're in refreshes, they should inherit from Sys.UI.Control, which takes takes an element in its constructor. When that element is replaced by the update panel, the client object will be disposed and then re-created. If you're currently using a ScriptComponentDescriptor on the server side to define the client control instance, you'll want to switch to a ScriptControlDescriptor.
By the sounds of it, your client objects just inherit from Sys.Component, which will hang around until they're manually disposed, which is why you're getting an error about having more than one component with the same ID.
I would advise against using a new ID every post back - this will just keep creating new client objects without ever cleaning up the old ones.