Have you tried turning off the Leave Warning alert only to find that despite repeated attempts to get rid of it, it seems to reset itself after a Preview? And do you find that this problem occurs more frequently for templates that make use of complex controls, like DataGrids and Calendars that require a post back to the server?
You've already set the
EnableLeaveAuthoringWarning attribute to
false on the Authoring Console. Or alternatively, you have implemented
Stefan's sample to turn off the leave warning for a particular control. Somehow, the warning goes away for a while, but after a Preview, the
EnableLeaveAuthoringWarning flag seems to reset itself and appears once again.
Try it:
1. Add an ASP.NET Button to a template file.
2. Set the
EnableLeaveAuthoringWarning flag of the Authoring Console to
false.
3. Click Preview.
4. Close the Preview window.
5. Click on the Button you have added to the template. The alert appears despite being disabled!
Last week, we finally found the reason. The reason lies in the
CMS_restoreAfterPreviewPostback() Javascript routine that is called after the Preview button is clicked.
In that routine (found in the Console.js file), the
g_bWarnBeforeLeave global variable, which decides whether or not the leave warning appears is reset to true:
function CMS_restoreAfterPreviewPostback()
{
// restore state of form. . . code continues . . .g_bWarnBeforeLeave = true;}We need to set it to false when
EnableLeaveAuthoringWarning is false. I'm not sure if modifying the Console.js file violates any support agreement with Microsoft. Anyway, it's probably not a good idea to do so as the next service pack will just overwrite all changes that we have done to it.
So to get around this, we will take advantage of the fact that the routine is written in a linked script file. This means that if we were to embed a routine of the same name within postings, our custom version will run instead of the linked routine.
We added the following code to the DefaultConsole.ascx.cs file. The script basically re-generates the
Cms_restoreAfterPreviewPostback() method, but instead of always setting the
g_bWarnBeforeLeave flag to
true, it is assigned the value of the Console's
EnableLeaveAuthoringWarning property. So if the leave warning is turned off, it stays off!
private void Page_Load(object sender, System.EventArgs e)
{
GeneratePreviewScript();
}
private void GeneratePreviewScript()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language=\"javascript\">\n");
sb.Append("function CMS_restoreAfterPreviewPostback()\n");
sb.Append("{\n");
sb.Append("// restore state of form\n");
sb.Append("__CMS_PostbackForm.action = g_strOriginalFormAction;\n");
sb.Append("__CMS_PostbackForm.target = g_strOriginalFormTarget;\n");
sb.Append("g_bWarnBeforeLeave = " + Console1.EnableLeaveAuthoringWarning.ToString().ToLower() + ";\n");
sb.Append("}\n");
sb.Append("</script>\n");
if(!Page.IsClientScriptBlockRegistered("WACConsolePreviewScript"))
{
Page.RegisterStartupScript("WACConsolePreviewScript",sb.ToString());
}
}
I'm not sure why the preview button resets the flag in the first place, but at least with this workaround, our users will continue to receive a pleasant authoring experience!