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