D
dcassar
I have built a custom validator control following the steps suggested
in the article "Developing a Validator Control"
(http://msdn.microsoft.com/library/d...uide/html/cpconsupportingup-levelbrowsers.asp).
The class derives from WebControl, implements IValidator, adds itself
into the Page.Validators collection during OnInit, and removes itself
during OnUnload. But for some strange reason, when the page is
rendered, the control is not included in the Page_Validators
collection. All of my other validators are there, but this one is not.
Two few things I'm doing that deviate from the norm: (1) I implemented
a custom control builder to support reading the embedded text in the
tag (like the ASP.NET validators do) and (2) I am validating a
collection of controls rather than a single control, so I do not expose
a ControlToValidate property.
Any suggestions?
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SCAPE.Web.Controls.Validation
{
/// <summary>
/// Validates that at least one checkbox has been checked.
/// </summary>
[ControlBuilder(typeof(SCAPE.Web.Controls.BusinessControls.ControlLabelControlBuilder))]
[ParseChildren(false)]
public class CheckBoxGroupValidator : WebControl, IValidator
{
private string _controlsToValidate = String.Empty;
private string _errorMessage = String.Empty;
private bool _enableClientScript = true;
private bool _isValid = true;
private string _text = String.Empty;
private ArrayList _innerControlsToValidate;
private ValidatorDisplay _display;
public CheckBoxGroupValidator()
{
ForeColor = System.Drawing.Color.Red;
Display = ValidatorDisplay.Static;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
{
Page.Validators.Add(this);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (EnableClientScript) RegisterScripts();
}
protected override void OnUnload(EventArgs e)
{
if (Page != null)
{
Page.Validators.Remove(this);
}
base.OnUnload(e);
}
private void RegisterScripts()
{
if (!Page.IsClientScriptBlockRegistered(GetType().ToString()))
{
System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("<script language=\"javascript\"
type=\"text/javascript\"> ");
script.Append("function CheckBoxGroupValidatorIsValid(val) { ");
script.Append("alert(\"validating \" + val.id); ");
script.Append("var ary = val.controlstovalidate.split(\",\"); ");
script.Append("var count = 0; ");
script.Append("for (var i = 0 ; i < ary.length ; i++) { ");
script.Append("alert(\"checking checkbox \" + ary); ");
script.Append("if (document.getElementById(ary).checked) return
true; ");
script.Append("} ");
script.Append("return false; ");
script.Append("} ");
script.Append("</script>");
Page.RegisterClientScriptBlock(GetType().ToString(),
script.ToString());
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("id", ClientID);
ControlStyle.AddAttributesToRender(writer, this);
RenderVisibilityAttributes(writer);
writer.AddAttribute("errormessage", ErrorMessage);
writer.AddAttribute("enabled", Enabled.ToString());
writer.AddAttribute("controlstovalidate",
GetClientSideControlsToValidate());
writer.AddAttribute("evaluationfunction",
"CheckBoxGroupValidatorIsValid");
}
private string GetClientSideControlsToValidate()
{
BuildControlArray();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0 ; i < InnerControlsToValidate.Count ; i++)
{
sb.Append(((Control)InnerControlsToValidate).ClientID + ((i <
InnerControlsToValidate.Count - 1) ? "," : ""));
}
return sb.ToString();
}
private void RenderVisibilityAttributes(HtmlTextWriter writer)
{
string displayAttribute;
string displayValue;
if (Display == ValidatorDisplay.Dynamic)
{
displayAttribute = "display";
displayValue = IsValid ? "none" : "inline";
}
else
{
displayAttribute = "visibility";
displayValue = IsValid ? "hidden" : "visible";
}
writer.AddStyleAttribute(displayAttribute, displayValue);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(Text);
}
public ValidatorDisplay Display
{
get { return _display; }
set { _display = value; }
}
public string ControlsToValidate
{
get { return _controlsToValidate; }
set { _controlsToValidate = value; }
}
public bool EnableClientScript
{
get { return _enableClientScript; }
set { _enableClientScript = value; }
}
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
protected virtual void OnValidate()
{
BuildControlArray();
ScanControls();
}
private void BuildControlArray()
{
if (_innerControlsToValidate == null)
{
_innerControlsToValidate = new ArrayList();
string[] nameArray = _controlsToValidate.Split(',');
foreach (string controlName in nameArray)
{
Control c = NamingContainer.FindControl(controlName);
if (c is CheckBox || c is HtmlInputCheckBox)
{
InnerControlsToValidate.Add(c);
}
else if (c == null)
{
throw new ApplicationException(String.Format("\"{0}\" is not a
valid control ID within the same naming container as this {1}",
controlName, GetType()));
}
else
{
throw new ApplicationException(String.Format("\"{0}\" is not a
CheckBox", controlName));
}
}
}
}
private void ScanControls()
{
IsValid = true;
foreach (Control c in InnerControlsToValidate)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
{
return;
}
}
else if (c is HtmlInputCheckBox)
{
if (((HtmlInputCheckBox)c).Checked)
{
return;
}
}
else
{
throw new ApplicationException("Unexpected error: control is not a
checkbox");
}
}
IsValid = false;
}
private ArrayList InnerControlsToValidate
{
get
{
return _innerControlsToValidate;
}
set
{
_innerControlsToValidate = value;
}
}
#region IValidator Members
public void Validate()
{
OnValidate();
}
public bool IsValid
{
get
{
return _isValid;
}
set
{
_isValid = value;
}
}
public string ErrorMessage
{
get
{
return _errorMessage;
}
set
{
_errorMessage = value;
}
}
#endregion
}
}
in the article "Developing a Validator Control"
(http://msdn.microsoft.com/library/d...uide/html/cpconsupportingup-levelbrowsers.asp).
The class derives from WebControl, implements IValidator, adds itself
into the Page.Validators collection during OnInit, and removes itself
during OnUnload. But for some strange reason, when the page is
rendered, the control is not included in the Page_Validators
collection. All of my other validators are there, but this one is not.
Two few things I'm doing that deviate from the norm: (1) I implemented
a custom control builder to support reading the embedded text in the
tag (like the ASP.NET validators do) and (2) I am validating a
collection of controls rather than a single control, so I do not expose
a ControlToValidate property.
Any suggestions?
using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace SCAPE.Web.Controls.Validation
{
/// <summary>
/// Validates that at least one checkbox has been checked.
/// </summary>
[ControlBuilder(typeof(SCAPE.Web.Controls.BusinessControls.ControlLabelControlBuilder))]
[ParseChildren(false)]
public class CheckBoxGroupValidator : WebControl, IValidator
{
private string _controlsToValidate = String.Empty;
private string _errorMessage = String.Empty;
private bool _enableClientScript = true;
private bool _isValid = true;
private string _text = String.Empty;
private ArrayList _innerControlsToValidate;
private ValidatorDisplay _display;
public CheckBoxGroupValidator()
{
ForeColor = System.Drawing.Color.Red;
Display = ValidatorDisplay.Static;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Page != null)
{
Page.Validators.Add(this);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (EnableClientScript) RegisterScripts();
}
protected override void OnUnload(EventArgs e)
{
if (Page != null)
{
Page.Validators.Remove(this);
}
base.OnUnload(e);
}
private void RegisterScripts()
{
if (!Page.IsClientScriptBlockRegistered(GetType().ToString()))
{
System.Text.StringBuilder script = new System.Text.StringBuilder();
script.Append("<script language=\"javascript\"
type=\"text/javascript\"> ");
script.Append("function CheckBoxGroupValidatorIsValid(val) { ");
script.Append("alert(\"validating \" + val.id); ");
script.Append("var ary = val.controlstovalidate.split(\",\"); ");
script.Append("var count = 0; ");
script.Append("for (var i = 0 ; i < ary.length ; i++) { ");
script.Append("alert(\"checking checkbox \" + ary); ");
script.Append("if (document.getElementById(ary).checked) return
true; ");
script.Append("} ");
script.Append("return false; ");
script.Append("} ");
script.Append("</script>");
Page.RegisterClientScriptBlock(GetType().ToString(),
script.ToString());
}
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
writer.AddAttribute("id", ClientID);
ControlStyle.AddAttributesToRender(writer, this);
RenderVisibilityAttributes(writer);
writer.AddAttribute("errormessage", ErrorMessage);
writer.AddAttribute("enabled", Enabled.ToString());
writer.AddAttribute("controlstovalidate",
GetClientSideControlsToValidate());
writer.AddAttribute("evaluationfunction",
"CheckBoxGroupValidatorIsValid");
}
private string GetClientSideControlsToValidate()
{
BuildControlArray();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0 ; i < InnerControlsToValidate.Count ; i++)
{
sb.Append(((Control)InnerControlsToValidate).ClientID + ((i <
InnerControlsToValidate.Count - 1) ? "," : ""));
}
return sb.ToString();
}
private void RenderVisibilityAttributes(HtmlTextWriter writer)
{
string displayAttribute;
string displayValue;
if (Display == ValidatorDisplay.Dynamic)
{
displayAttribute = "display";
displayValue = IsValid ? "none" : "inline";
}
else
{
displayAttribute = "visibility";
displayValue = IsValid ? "hidden" : "visible";
}
writer.AddStyleAttribute(displayAttribute, displayValue);
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(Text);
}
public ValidatorDisplay Display
{
get { return _display; }
set { _display = value; }
}
public string ControlsToValidate
{
get { return _controlsToValidate; }
set { _controlsToValidate = value; }
}
public bool EnableClientScript
{
get { return _enableClientScript; }
set { _enableClientScript = value; }
}
public string Text
{
get
{
return _text;
}
set
{
_text = value;
}
}
protected virtual void OnValidate()
{
BuildControlArray();
ScanControls();
}
private void BuildControlArray()
{
if (_innerControlsToValidate == null)
{
_innerControlsToValidate = new ArrayList();
string[] nameArray = _controlsToValidate.Split(',');
foreach (string controlName in nameArray)
{
Control c = NamingContainer.FindControl(controlName);
if (c is CheckBox || c is HtmlInputCheckBox)
{
InnerControlsToValidate.Add(c);
}
else if (c == null)
{
throw new ApplicationException(String.Format("\"{0}\" is not a
valid control ID within the same naming container as this {1}",
controlName, GetType()));
}
else
{
throw new ApplicationException(String.Format("\"{0}\" is not a
CheckBox", controlName));
}
}
}
}
private void ScanControls()
{
IsValid = true;
foreach (Control c in InnerControlsToValidate)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
{
return;
}
}
else if (c is HtmlInputCheckBox)
{
if (((HtmlInputCheckBox)c).Checked)
{
return;
}
}
else
{
throw new ApplicationException("Unexpected error: control is not a
checkbox");
}
}
IsValid = false;
}
private ArrayList InnerControlsToValidate
{
get
{
return _innerControlsToValidate;
}
set
{
_innerControlsToValidate = value;
}
}
#region IValidator Members
public void Validate()
{
OnValidate();
}
public bool IsValid
{
get
{
return _isValid;
}
set
{
_isValid = value;
}
}
public string ErrorMessage
{
get
{
return _errorMessage;
}
set
{
_errorMessage = value;
}
}
#endregion
}
}