No client side script generated by custom validator

Discussion in 'ASP .Net' started by Stephen Adam, Feb 22, 2005.

  1. Stephen Adam

    Stephen Adam Guest

    Hi there people,

    I'm creating my own set of self validating text boxes. They all
    inherit from a base text box which simply contains a required field
    validator, they then usuallly implement another validator to check
    they contain number, currencies, dates or whatever.

    The problem i'm having is with the DateTextBox, I have created my own
    Date Validator which inherits from the BaseValidator class which the
    DateTextBox uses. My problem is that no validation is taking place on
    the client side. I've included the code for all related parts, if you
    need anymore info then please just let me know. Thanks

    steve


    <table>

    <tr>
    <td> Date Text Box </td>
    <td width="15px"> </td>
    <td> <CustomWebControls:DateTextBox id="tbDate" runat="server"
    /></td>
    </tr>

    </table>

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;
    using System.Globalization;

    namespace CustomWebControls
    {

    public class DateValidator : BaseValidator
    {


    protected override bool ControlPropertiesValid()
    {
    Control ctrl;
    ctrl = FindControl(ControlToValidate);

    if (ctrl == null)
    {
    return false;
    }
    else
    {
    return true;
    }
    }

    protected override bool EvaluateIsValid()
    {

    string strInputDate;
    DateTime parsedDate = new DateTime();

    CultureInfo cltrInfo = new CultureInfo("en-GB", false);
    DateTimeFormatInfo dtFormat = cltrInfo.DateTimeFormat;

    strInputDate = (this.GetControlValidationValue(ControlToValidate));

    try
    {
    parsedDate = DateTime.ParseExact(strInputDate,
    dtFormat.ShortDatePattern, dtFormat);
    }
    catch ( System.Exception )
    {
    return false;
    }

    return true;
    }
    }
    }


    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace CustomWebControls
    {
    public class BaseTextBox : System.Web.UI.WebControls.TextBox
    {
    private RequiredFieldValidator valRequired;

    public bool allowBlank = true;
    public string ClientScript="true";

    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);

    if (allowBlank == false)
    {
    AddRequiredValidator();
    }

    }

    protected void AddRequiredValidator()
    {

    valRequired = new RequiredFieldValidator();
    valRequired.ErrorMessage = "Error Field Cannot be Empty! ";
    valRequired.ControlToValidate = this.ID;
    valRequired.EnableClientScript =
    (this.ClientScript.ToLower()!="false");
    valRequired.Display =
    System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
    Controls.Add(valRequired);
    }


    protected override void Render(HtmlTextWriter writer)
    {
    base.Render (writer);

    if (allowBlank == false)
    {
    valRequired.RenderControl(writer);
    }
    }
    }

    }



    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;

    namespace CustomWebControls
    {

    public class DateTextBox : BaseTextBox
    {

    private DateValidator valIsDate;


    protected override void OnInit(EventArgs e)
    {
    base.OnInit (e);
    AddDateValidator();
    }

    private void AddDateValidator()
    {
    valIsDate = new DateValidator();
    valIsDate.ErrorMessage = " Date must be in form dd/mm/yyyy";
    valIsDate.ErrorMessage.PadLeft(2, ' ');
    valIsDate.ControlToValidate = this.ID;
    valIsDate.EnableClientScript =
    (this.ClientScript.ToLower()!="false");
    valIsDate.Display =
    System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
    Controls.Add(valIsDate);
    }

    protected override void Render(HtmlTextWriter writer)
    {
    base.Render (writer);
    valIsDate.RenderControl(writer);
    }

    }
    }
    Stephen Adam, Feb 22, 2005
    #1
    1. Advertising

  2. Stephen Adam

    Peter Blum Guest

    Please see my answer to this same question you posted on the www.asp.net
    forums.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "Stephen Adam" <> wrote in message
    news:...
    > Hi there people,
    >
    > I'm creating my own set of self validating text boxes. They all
    > inherit from a base text box which simply contains a required field
    > validator, they then usuallly implement another validator to check
    > they contain number, currencies, dates or whatever.
    >
    > The problem i'm having is with the DateTextBox, I have created my own
    > Date Validator which inherits from the BaseValidator class which the
    > DateTextBox uses. My problem is that no validation is taking place on
    > the client side. I've included the code for all related parts, if you
    > need anymore info then please just let me know. Thanks
    >
    > steve
    >
    >
    > <table>
    >
    > <tr>
    > <td> Date Text Box </td>
    > <td width="15px"> </td>
    > <td> <CustomWebControls:DateTextBox id="tbDate" runat="server"
    > /></td>
    > </tr>
    >
    > </table>
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    > using System.Globalization;
    >
    > namespace CustomWebControls
    > {
    >
    > public class DateValidator : BaseValidator
    > {
    >
    >
    > protected override bool ControlPropertiesValid()
    > {
    > Control ctrl;
    > ctrl = FindControl(ControlToValidate);
    >
    > if (ctrl == null)
    > {
    > return false;
    > }
    > else
    > {
    > return true;
    > }
    > }
    >
    > protected override bool EvaluateIsValid()
    > {
    >
    > string strInputDate;
    > DateTime parsedDate = new DateTime();
    >
    > CultureInfo cltrInfo = new CultureInfo("en-GB", false);
    > DateTimeFormatInfo dtFormat = cltrInfo.DateTimeFormat;
    >
    > strInputDate = (this.GetControlValidationValue(ControlToValidate));
    >
    > try
    > {
    > parsedDate = DateTime.ParseExact(strInputDate,
    > dtFormat.ShortDatePattern, dtFormat);
    > }
    > catch ( System.Exception )
    > {
    > return false;
    > }
    >
    > return true;
    > }
    > }
    > }
    >
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomWebControls
    > {
    > public class BaseTextBox : System.Web.UI.WebControls.TextBox
    > {
    > private RequiredFieldValidator valRequired;
    >
    > public bool allowBlank = true;
    > public string ClientScript="true";
    >
    > protected override void OnInit(EventArgs e)
    > {
    > base.OnInit(e);
    >
    > if (allowBlank == false)
    > {
    > AddRequiredValidator();
    > }
    >
    > }
    >
    > protected void AddRequiredValidator()
    > {
    >
    > valRequired = new RequiredFieldValidator();
    > valRequired.ErrorMessage = "Error Field Cannot be Empty! ";
    > valRequired.ControlToValidate = this.ID;
    > valRequired.EnableClientScript =
    > (this.ClientScript.ToLower()!="false");
    > valRequired.Display =
    > System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
    > Controls.Add(valRequired);
    > }
    >
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > base.Render (writer);
    >
    > if (allowBlank == false)
    > {
    > valRequired.RenderControl(writer);
    > }
    > }
    > }
    >
    > }
    >
    >
    >
    > using System;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.ComponentModel;
    >
    > namespace CustomWebControls
    > {
    >
    > public class DateTextBox : BaseTextBox
    > {
    >
    > private DateValidator valIsDate;
    >
    >
    > protected override void OnInit(EventArgs e)
    > {
    > base.OnInit (e);
    > AddDateValidator();
    > }
    >
    > private void AddDateValidator()
    > {
    > valIsDate = new DateValidator();
    > valIsDate.ErrorMessage = " Date must be in form dd/mm/yyyy";
    > valIsDate.ErrorMessage.PadLeft(2, ' ');
    > valIsDate.ControlToValidate = this.ID;
    > valIsDate.EnableClientScript =
    > (this.ClientScript.ToLower()!="false");
    > valIsDate.Display =
    > System.Web.UI.WebControls.ValidatorDisplay.Dynamic;
    > Controls.Add(valIsDate);
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > base.Render (writer);
    > valIsDate.RenderControl(writer);
    > }
    >
    > }
    > }
    Peter Blum, Feb 22, 2005
    #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. Magnus Blomberg
    Replies:
    3
    Views:
    717
    Magnus Blomberg
    Apr 14, 2005
  2. Alan Silver
    Replies:
    2
    Views:
    37,133
    Alan Silver
    Nov 2, 2005
  3. Boss302
    Replies:
    0
    Views:
    1,037
    Boss302
    Nov 21, 2006
  4. Jay Douglas
    Replies:
    0
    Views:
    524
    Jay Douglas
    Feb 27, 2007
  5. Zoe Hart
    Replies:
    1
    Views:
    365
    Scott Wisniewski
    Jan 8, 2004
Loading...

Share This Page