Displaying a JavaScript confirm dialog on Document.MoveToTrash
I would like to display a JavaScript confirm dialog whenever a user makes a change to a document of a certain type. This works for the Document.BeforePublish event with the following code:
namespace MyNamespace {
public class UmbracoEventHandlers : ApplicationBase { public UmbracoEventHandlers() { Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish); Document.BeforeMoveToTrash += new Document.MoveToTrashEventHandler(Document_BeforeMoveToTrash); } void Document_BeforeMoveToTrash(Document sender, MoveToTrashEventArgs e) { ShowConfirm(sender); } void Document_BeforePublish(Document sender, PublishEventArgs e) { ShowConfirm(sender); } /* Output JavaScript showing a confirm dialog. */ void ShowConfirm(Document sender) { if (sender.ContentType.MasterContentType == 1042) { StringBuilder sb = new StringBuilder("<script type=\"text/javascript\">"); sb.AppendLine("var answer = confirm('Question')"); sb.AppendLine("if (answer){"); sb.AppendLine(" $.get('/Base/RestExtensions/DoSomething/" + sender.Id.ToString() + ".aspx');"); sb.AppendLine("}"); sb.AppendLine("</script>");
When the BeforePublish event is triggered, a confirm dialog pops up when the document is published. However, when deleting a document, the event fires, but no confirm dialog appears. Instead, the following exception is thrown on the line where the ClientScriptManager is initialized:
Unable to cast object of type 'HandlerWrapper' to type 'umbraco.BasePages.BasePage'.
I suspect this has something to do with the HttpContext being handled differently when deleting compared to publishing. Is there another way I can manage to inject the JavaScript also when deleting?
The thing is, I am able to get the request handler when publishing (Document.BeforePublish); but not on Document.MoveToTrash. Any thoughts as to why this is handled differently on these two events?
If you or anyone else have any other suggestions as to how I might be able to add the JavaScript to the page, it would be greatly appreciated. Is it, for example, possible to redirect to the admin interface with some querystring parameters (http://mypage.com/umbraco/?action=ShowConfirm&documentId=1234), and then add the JavaScript to the admin page when it is rendered using the data from the querystring?
thank you for your answer, but as I said, this works fine for publishing. The problem is related to deleting (Document.MoveToTrash). Besides, your suggestion would only work when right clicking the node in the tree and choosing publish, and would apply to all document types.
So I'm still looking for a solution to my problem. Any other suggestions to how I might be able to display a confirm dialog? Using Umbraco's ClientTools.OpenModalWindow is not working either, since this is also depending on an IHttpHandler.
Although the best thing would be to retrieve the current handler somehow like when publishing, it would be sufficient if I could be able to be able to write the JavaScript stub to the page in some other way.
Even if you inject some javascript, I'm not sure how you're going to hook it into the "Delete" context menu action. You might need to just write your own Action and use that in place of the standard "Delete" action.
Displaying a JavaScript confirm dialog on Document.MoveToTrash
I would like to display a JavaScript confirm dialog whenever a user makes a change to a document of a certain type. This works for the Document.BeforePublish event with the following code:
When the BeforePublish event is triggered, a confirm dialog pops up when the document is published. However, when deleting a document, the event fires, but no confirm dialog appears. Instead, the following exception is thrown on the line where the ClientScriptManager is initialized:
I suspect this has something to do with the HttpContext being handled differently when deleting compared to publishing. Is there another way I can manage to inject the JavaScript also when deleting?
Hi. It looks like that the publishing process runs in the context of an ajax request. So that the actual request handler is not a page at all.
Hi Rodion.
The thing is, I am able to get the request handler when publishing (Document.BeforePublish); but not on Document.MoveToTrash. Any thoughts as to why this is handled differently on these two events?
If you or anyone else have any other suggestions as to how I might be able to add the JavaScript to the page, it would be greatly appreciated. Is it, for example, possible to redirect to the admin interface with some querystring parameters (http://mypage.com/umbraco/?action=ShowConfirm&documentId=1234), and then add the JavaScript to the admin page when it is rendered using the data from the querystring?
I'm not sure if it solves your problem, but if you want just something like confirmation then it's possible just to modify sources a bit.
In the file ~/umbraco/dialogs/publish.aspx replace the string (at the line 88)
to something like:
or like that
Rodion,
thank you for your answer, but as I said, this works fine for publishing. The problem is related to deleting (Document.MoveToTrash). Besides, your suggestion would only work when right clicking the node in the tree and choosing publish, and would apply to all document types.
So I'm still looking for a solution to my problem. Any other suggestions to how I might be able to display a confirm dialog? Using Umbraco's ClientTools.OpenModalWindow is not working either, since this is also depending on an IHttpHandler.
Although the best thing would be to retrieve the current handler somehow like when publishing, it would be sufficient if I could be able to be able to write the JavaScript stub to the page in some other way.
Hi,
Even if you inject some javascript, I'm not sure how you're going to hook it into the "Delete" context menu action. You might need to just write your own Action and use that in place of the standard "Delete" action.
Hope this helps,
Tom
is working on a reply...