Custom Validator Control

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 ----------------------------------
.....
<asp:placeholder id="TermsAndConditionsCv" Runat="server"></asp:placeholder>
.....
------------------- 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;

}

}

}
 
W

William F. Robertson, Jr.

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 said:
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 ----------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top