Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Custom Validator Control

Reply
Thread Tools

Custom Validator Control

 
 
DotNetGruven
Guest
Posts: n/a
 
      02-19-2004
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(checkBoxValidato r);

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;

}

}

}


 
Reply With Quote
 
 
 
 
William F. Robertson, Jr.
Guest
Posts: n/a
 
      02-19-2004
Are you trying to stop submission client side? You will need to output some
javascript to work client side.

Also the control isn't being added to the page until PreRender which is way
after any event processing, so you will miss out on that server side.

HTH,
bill


"DotNetGruven" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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(checkBoxValidato r);
>
> 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;
>
> }
>
> }
>
> }
>
>



 
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
Struts Validator - creditCard validator smrimell@gmail.com Java 4 02-13-2007 08:32 PM
Re: Adding Validator Control at runtime in a Custom Control mudasir.ahmed@gmail.com ASP .Net 1 12-17-2004 09:14 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
How to get custom control to work with custom validator Scott Yenor ASP .Net Web Controls 0 01-27-2004 12:37 AM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM



Advertisments