D
DotNetGruven
Can I create a custom validator control in my project and add it to a
PlaceHolder on my ASPX page??
Here is what I've tried. It compiles and runs without error, but doesn't
detected that the checkbox it is validating is checked when the form is
submitted.
TIA,
George
------------------ from target ASPX page ----------------------------------
.....
<asplaceholder id="TermsAndConditionsCv" Runat="server"></asplaceholder>
.....
------------------- from target ASPX.CS
page --------------------------------
protected override void OnPreRender(EventArgs e)
{
Components.CheckBoxValidator checkBoxValidator = new
MobileGuardianWeb.Components.CheckBoxValidator();
checkBoxValidator.ControlToValidate = TermsAndConditions.ID;
checkBoxValidator.ErrorMessage = "You must agree to our terms and
condtions";
checkBoxValidator.Text = "*";
checkBoxValidator.ForeColor = System.Drawing.Color.Red;
checkBoxValidator.Display = ValidatorDisplay.Dynamic;
TermsAndConditionsCv.Controls.Add(checkBoxValidator);
base.OnPreRender (e);
}
------------------- source for validator:
Components/CheckBoxValidator.cs --------------------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyProject.Components
{
/// <summary>
/// Summary description for CheckBoxValidator.
/// </summary>
public class CheckBoxValidator: BaseValidator
{
private CheckBox _checkbox = null;
public CheckBoxValidator()
{
base.EnableClientScript = true;
}
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_checkbox = (CheckBox) ctrl;
return (_checkbox != null);
}
else
return false; // raise exception
}
protected override bool EvaluateIsValid()
{
return _checkbox.Checked;
}
}
}
PlaceHolder on my ASPX page??
Here is what I've tried. It compiles and runs without error, but doesn't
detected that the checkbox it is validating is checked when the form is
submitted.
TIA,
George
------------------ from target ASPX page ----------------------------------
.....
<asplaceholder id="TermsAndConditionsCv" Runat="server"></asplaceholder>
.....
------------------- from target ASPX.CS
page --------------------------------
protected override void OnPreRender(EventArgs e)
{
Components.CheckBoxValidator checkBoxValidator = new
MobileGuardianWeb.Components.CheckBoxValidator();
checkBoxValidator.ControlToValidate = TermsAndConditions.ID;
checkBoxValidator.ErrorMessage = "You must agree to our terms and
condtions";
checkBoxValidator.Text = "*";
checkBoxValidator.ForeColor = System.Drawing.Color.Red;
checkBoxValidator.Display = ValidatorDisplay.Dynamic;
TermsAndConditionsCv.Controls.Add(checkBoxValidator);
base.OnPreRender (e);
}
------------------- source for validator:
Components/CheckBoxValidator.cs --------------------------------------
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyProject.Components
{
/// <summary>
/// Summary description for CheckBoxValidator.
/// </summary>
public class CheckBoxValidator: BaseValidator
{
private CheckBox _checkbox = null;
public CheckBoxValidator()
{
base.EnableClientScript = true;
}
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_checkbox = (CheckBox) ctrl;
return (_checkbox != null);
}
else
return false; // raise exception
}
protected override bool EvaluateIsValid()
{
return _checkbox.Checked;
}
}
}