How to disable the "Leave Authoring" warning for a specific control rather than for the whole template (last update: 08/12/2004)

Developing my Property Authoring Server Control I run into the issue that using the Calendar control during Authoring time caused the “Are you sure to navigate away from this page?” dialog to show up.

As it would have been an ugly idea to force the user to disable the warning message completly using the EnableLeaveAuthoringWarning console property I looked into the details why this warning shows up when such a postback is fired.

I identified that the reason for this is that the postback is issued by the href attribute of the <a href=…> link. When javascript code is defined in this property the onbeforeunload event is raised which is configured to show this warning.

So the problem is that using href in anchor tags will not allow the client side code to be executed that checks if the current action is actually a postback or if the user really decided to leave the page. If the redirect would have been done using the onclick attribute rather than the href this would not have happend.

The solution is to change this anchor tag in a way that the code for the postback is in the onclick attribute rather than in the href attribute. If the control is embedded in a custom control (as it is with my Property Authoring Server Control) this can be achieved by overriding the Render method and to adjust the anchor tags.

In my Property Authoring Server Control I did this by implementing the following Render method.

  protected override void Render(System.Web.UI.HtmlTextWriter output)
   // only do a change if in Authoring mode
   if (((WebAuthorContext.Current.Mode == WebAuthorContextMode.AuthoringReedit)
    ||(WebAuthorContext.Current.Mode == WebAuthorContextMode.AuthoringNew)))
    TextWriter tempWriter = new StringWriter();
    base.Render(new System.Web.UI.HtmlTextWriter(tempWriter));
    string orightml= tempWriter.ToString();

    // correct the “leaving the page problem”.
    Regex hrefRegex = new Regex(“href=\”javascript(?<PostBackScript>.*?)\””, RegexOptions.Singleline);
    string newhtml = hrefRegex.Replace(orightml, “href=\”#\” onclick=\”${PostBackScript};return false;\””);



  1. I changed the code to correct the hyperlink based on a suggestion from Adam Creeger.

    Thanks Adam for your hint!




  2. Mei Ying and myself have already blogged about this topic.

    Chester now added a great summary plus an…


  3. In the first part of this article I discussed how to implement a SiteMapProvider for an MCMS website….


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.