Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Microsoft Content Management Server : Preventing Pages with Invalid Content from Being Saved

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
1/21/2013 11:02:17 AM

There’s still a problem to be tackled. Although the custom validation control has all the necessary code, it won’t work unless someone clicks on an ASP.NET control, such as a Button, that has its Control.CausesValidation property set to true. Otherwise, the page will still be saved even if required placeholders are not filled in.

If you looked at the way the controls on the Default Console are coded (in the Defaultconsole.ascx file), you will find that they are simply HTML anchor tags. HTML anchor tags are not buttons and therefore do not trigger page validations. As a result, clicking on any of the action links on the console will not fire our custom validation control.

We need the validator to fire when the user clicks one of the following three action buttons:

  • Save New Page

  • Save

  • Save and Exit

We will modify the behavior of each of the three buttons to cancel the save action when the page is not valid.

To find out how to force each of these controls to validate the page when clicked, we will once again get clues from ASP.NET. Earlier, we saw how ASP.NET generates a Button control:

<input type="submit"
       name="Button1"
      value="Button"
    onclick="if (typeof(Page_ClientValidate)=='function')
Page_ClientValidate();"
   language="javascript"
         id="Button1" />

The highlighted line shows the code used to ensure that client-side validation takes place before the form is submitted. It basically calls the Page_ClientValidation() client-side script of the WebUIValidation.js file. This line of code ensures our HTML anchors will exhibit the same behavior as a Button.

We need to insert it before calling the client-side save methods, which means we should modify each of the three Console Action buttons, starting with the Save New Page control.

Save New Page with Validation

Add a class file named ValidatedSaveNewAction.cs to the MCMSValidators project. Above the namespace declaration, add the following required namespaces:

using System;
using Microsoft.ContentManagement.Publishing;
						using Microsoft.ContentManagement.WebControls;
						using Microsoft.ContentManagement.WebControls.ConsoleControls;
namespace MCMSValidators
{
}

Instead of re-writing the Save New Page action control from scratch, we will create it by inheriting from the AuthoringSaveNewAction class:

public class ValidatedSaveNewAction : AuthoringSaveNewAction
{
}

The only modification we need to make is to override the ActionJavascript property. We first check to see if the control is available to the user. If it is, we look for the presence of validation controls using the Page.Validators.Count property. Once we have ascertained that there are validation controls present, we inject the JavaScript statement to call the Page_ClientValidate() method.

Below the ValidatedSaveNewAction() constructor, add the overridden ActionJavascript property:

public override string ActionJavascript
{
  get
  {
    string strReturn = null;
    if (this.Available)
    {
      // Check to see if we have any validation controls on this page
      if (this.Page.Validators.Count > 0)
      {
        strReturn += "if (typeof(Page_ClientValidate) == 'function') ";
        strReturn += "{";
        strReturn += "     if (!Page_ClientValidate()) return false;";
        strReturn += "}";
      }
      strReturn += base.ActionJavascript;
    }
    return strReturn;
  }
}

Save and Exit with Validation

The next button that we will modify is the Save and Exit button. Add a class file named ValidatedReeditSaveAndExitAction.cs to the MCMSValidators project. Insert the required namespaces as shown below:

using System;
using Microsoft.ContentManagement.Publishing;
						using Microsoft.ContentManagement.WebControls;
						using Microsoft.ContentManagement.WebControls.ConsoleControls;
namespace MCMSValidators
{
}

Like the Save New Page action control, we will modify the Save and Exit control by inheriting directly from the AuthoringReeditSaveAndExitAction class.

public class ValidatedReeditSaveAndExitAction :
AuthoringReeditSaveAndExitAction
{
}

We will override the ActionJavascript method in exactly the same way as for the Save New Page action control. Add the ActionJavascript method directly below the ValidatedReeditSaveAndExitAction() constructor:

public override string ActionJavascript
{
  get
  {
    string strReturn = null;
    if (this.Available)
    {
      // Check to see if we have any validation controls on this page
      if (this.Page.Validators.Count > 0)
      {
        strReturn += "if (typeof(Page_ClientValidate) == 'function') ";
        strReturn += "{";
        strReturn += "     if (!Page_ClientValidate()) return false;";
        strReturn += "}";
      }
      strReturn += base.ActionJavascript;
    }
    return strReturn;
  }
}

Save with Validation

Finally, we will modify the Save button. Add another class to the MCMSValidators project. Name the new class file ValidatedReeditSaveAction.cs. The implementation of this class file is exactly the same as the earlier two except that it inherits from the AuthoringReeditSaveAction class. The code below shows the complete code file:

using System;
using Microsoft.ContentManagement.Publishing;
using Microsoft.ContentManagement.WebControls;
using Microsoft.ContentManagement.WebControls.ConsoleControls;
namespace MCMSValidators
{
  public class ValidatedReeditSaveAction : AuthoringReeditSaveAction
  {
    public ValidatedReeditSaveAction()
    {}
    public override string ActionJavascript
    {
      get
      {
        string strReturn = null;
        if (this.Available)
        {
          // Check to see if we have any validation controls on this page
          if (this.Page.Validators.Count > 0)
          {
            strReturn += "if (typeof(Page_ClientValidate) == 'function') ";
            strReturn += "{";
            strReturn += "     if (!Page_ClientValidate()) return false;";
            strReturn += "}";
          }
          strReturn += base.ActionJavascript;
        }
        return strReturn;
      }
    }
  }
}

					  

The three modified console action buttons are complete. Save and compile the solution.

Adding the Modified Buttons to the Authoring Console

Now that we have modified the three buttons that trigger a save, we can use them to replace the ones on the Default Console by updating the DefaultConsole.ascx file. Before we do, it is a good idea to make a backup of it so we can always revert to a working version.

Open the DefaultConsole.ascx file in HTML view (you can find it in the Console folder of your MCMS web application project). At the top of the file, right after the <% Control %> directive, add the following statement:

<%@ Register TagPrefix="ValidatedControls"
    Namespace="MCMSValidators" Assembly="MCMSValidators" %>

Locate each of the save buttons to be replaced, starting with the Save New Page button. Look for the following block of code:

<CmsConsole:AuthoringSaveNewAction id="AuthoringSaveNewAction1"
   runat="server">
<A id="AuthoringSaveNewAnchor" href="#" target=_self
   onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR>
</CmsConsole:AuthoringSaveNewAction>

Replace it with this code that uses our modified Save New Page action control:

<ValidatedControls:ValidatedSaveNewAction id="ValidatedSaveNewAction1"
   runat="server">
<A id="ValidatedSaveNewAnchor" href="#" target=_self
   onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR>
</ValidatedControls:ValidatedSaveNewAction>

Next, replace the Save and Save and Exit buttons. Locate the following block of code (hint: search for the string AuthoringReeditSaveAction):

<CmsConsole:AuthoringReeditSaveAction id="AuthoringReeditSaveAction1"
   runat="server">
<A id="AuthoringReeditSaveAnchor" href="#" target=_self
   onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR />
</CmsConsole:AuthoringReeditSaveAction>
<CmsConsole:AuthoringReeditSaveAndExitAction
           id="AuthoringReeditSaveAndExitAction1" runat="server">
<A id="AuthoringReeditSaveAndExitAnchor" href="#" target=_self
   onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR />
</CmsConsole:AuthoringReeditSaveAndExitAction>

Replace it with the following that uses the modified buttons for Save and Save and Exit:

<ValidatedControls:ValidatedReeditSaveAction id="ValidatedReeditSaveAction1"
   runat="server">
<A id="ValidatedReeditSaveAnchor" href="#" target=_self
   onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR />
</ValidatedControls:ValidatedReeditSaveAction>
						<ValidatedControls:ValidatedReeditSaveAndExitAction
						id="ValidatedReeditSaveAndExitAction1" runat="server">
						<A id="ValidatedReeditSaveAndExitAnchor" href="#" target=_self
						onclick="<%# Container.ActionJavascript %>;return false" >
<%# Container.Text %>
</A>
<BR />
</ValidatedControls:ValidatedReeditSaveAndExitAction>

					  

The new console actions are now active.

The next time you attempt to save the page without first entering something into the HtmlPlaceholderControl that is validated, the page will not be saved, and an error message will be produced on the client without a round trip to the server.

Other -----------------
- Microsoft Systems Management Server 2003 : Permissions and Security Objects (part 2) - Assigning Permissions
- Microsoft Systems Management Server 2003 : Permissions and Security Objects (part 1)
- Microsoft Systems Management Server 2003 : Security - Accounts and Groups
- Windows Server 2003 on HP ProLiant Servers : Assessment of the Enterprise - Conducting the Assessment
- Windows Server 2003 on HP ProLiant Servers : Assessment of the Enterprise - The Assessment Team
- Windows Small Business Server 2011 : Disaster Planning - Preparing for a Disaster, Restoring from Backup
- Windows Small Business Server 2011 : Disaster Planning - Planning for Disaster
- SQL Server 2008 : Security - Networking
- SQL Server 2008 : Security - Authentication mode
- Microsoft Dynamic GP 2010 : Providing clean vendor information by properly closing Purchase Orders, Protecting against information loss by printing Fixed Asset Reports
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
programming4us
Natural Miscarriage
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Game Trailer