No client side script generated by custom validator

S

Stephen Adam

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);
}

}
}
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top