By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

Custom Validator Control

P: n/a
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(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;

}

}

}
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <ms********@javagruven.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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(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;

}

}

}

Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.