Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Page_Validators ignores my control

Reply
Thread Tools

Page_Validators ignores my control

 
 
dcassar@cmcdataworks.com
Guest
Posts: n/a
 
      06-28-2005
I have built a custom validator control following the steps suggested
in the article "Developing a Validator Control"
(http://msdn.microsoft.com/library/de...elbrowsers.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.BusinessC ontrols.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().ToS tring()))
{
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[i]); ");
script.Append("if (document.getElementById(ary[i]).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[i]).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
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding attribute to JavaScript validator object in the Page_Validators array Jim Ox ASP .Net 1 06-22-2006 07:54 PM
Page_Validators error Dan Hurwitz ASP .Net 1 10-19-2004 08:28 PM
Page_Validators is undefined Jscript runtime error mag48 ASP .Net 0 02-09-2004 03:09 PM
Page_Validators is undefined Ravikanth[MVP] ASP .Net 2 08-27-2003 11:10 AM
JavaScript Error: 'Page_Validators' is undefined DesignerX ASP .Net 2 07-25-2003 02:45 AM



Advertisments