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.