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 n开发者_StackOverflow社区ew 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...
When the page unloads, my component's dispose()
method is called and Sys.Application.removeComponent()
is also called. When the UpdatePanel
reloads, only dispose()
method is called. For now I have solved this by putting Sys.Application.removeComponent(this);
inside the dispose()
. 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!
精彩评论