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

No client side script generated by custom validator

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

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


P: n/a
Please see my answer to this same question you posted on the www.asp.net
forums.

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

"Stephen Adam" <00******@brookes.ac.uk> wrote in message
news:94**************************@posting.google.c om...
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);
}

}
}

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.