How to refresh a Sandboxed web part after leaving Edit Display Mode?
-
06-12-2019 - |
Question
I have a test SP page hosting a Sandbox WebPart, code only. The WebPart in turn contains a GridView.
I'd like to refresh the WebPart (actually, to fire the GridView-ObjectContainerDataSource selection process) after the user has done with modifying the WebPart, i.e after s/he has clicked OK in the (standard, non custom) right editor pane.
I've looked and tried with:
- overriding WebPart.OnEditModeChanged, but that was invoked only upon entering edit mode, not upon leaving it
- registering to WebPartManager.DisplayModeChanged during my web part's OnInit method, but that was never invoked in my tests.
I've searched on the web, but apparently everyone explains how to set the displaymode, and no one how to detect its changes :).
Any help? TA
Edit
I played a little bit with WebPart.OnEditModeChanged and ViewState. The problem is still due to OnEditModeChanged being called only on the first transition Display->Edit, then never again. This makes it impossible (I think) to track state when user does Display-Edit-Apply-Ok for example.
Solution
You could add a class which inherits from ToolPart, and override the ApplyChanges() class to do whatever extra you need, which is fired when OK is clicked and before the page is prepped to be reloaded.
You'll then need to add that custom ToolPart class the other ToolParts added to the web part (done by overriding GetToolParts() in your web part class).
OTHER TIPS
Have you tried to override the OnPreRender
method and rebinding the GridView
there? The OnPreRender
method is one of the last methods to be called in the aspx page / control lifecycle.
And perhaps checking for IsPostBack to fire only on postbacks (i.e. OK click in editor).
P.S. this is a very commonly asked question (not only with regards to controls / webparts in SharePoint), but ASP.NET in general. The image below illustrates how the flow of methods is in regular (non MVC) ASP.NET: