Custom Validator Control

Discussion in 'ASP .Net' started by DotNetGruven, Feb 19, 2004.

  1. DotNetGruven

    DotNetGruven Guest

    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;

    }

    }

    }
     
    DotNetGruven, Feb 19, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:%...
    > 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;
    >
    > }
    >
    > }
    >
    > }
    >
    >
     
    William F. Robertson, Jr., Feb 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pham Nguyen

    Custom Textbox and Validator Control

    Pham Nguyen, Jan 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    10,679
    Pham Nguyen
    Jan 11, 2004
  2. Replies:
    1
    Views:
    594
    Elmer CarĂ­as
    Dec 17, 2004
  3. Replies:
    4
    Views:
    905
    Richter~9.6
    Feb 13, 2007
  4. Matt Sokol
    Replies:
    2
    Views:
    390
    Theo Tillotson
    Aug 7, 2003
  5. Scott Yenor

    How to get custom control to work with custom validator

    Scott Yenor, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    213
    Scott Yenor
    Jan 27, 2004
Loading...

Share This Page