Ok, maybe I missed something somewhere, so here's some (abbreviated) code:
[DefaultProperty("Text")]
[ToolboxData("<{0}:DateValidator runat=server></{0}:DateValidator>")]
public class DateValidator : BaseValidator
{
protected override bool ControlPropertiesValid()
{
if(String.IsNullOrEmpty(ComparisonControlToValidate))
{
Control ctrl = Page.FindControl(ControlToValidate);
return (ctrl != null);
}
else
{
Control ctrl = Page.FindControl(ControlToValidate);
Control compCtrl = Page.FindControl(ComparisonControlToValidate);
return (ctrl != null && compCtrl != null);
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//add CSS.
string cssLink = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"DAVCustomControls.DateValidator.DateValidator_Styles.css");
Literal litCSS = new Literal();
litCSS.ID = this.ID + "__litCSS_DV";
litCSS.Text = "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + cssLink + "\" />";
if (this.Page.Header.FindControl("litCSS_DV") == null)
{
this.Page.Header.Controls.Add(litCSS);
}
}
protected override void OnPreRender(EventArgs e)
{
//register our client script for usual post-back
//(will do nothing in case of partial udpate)
string scriptUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"DAVCustomControls.DateValidator.DateValidator.js");
if(!Page.ClientScript.IsClientScriptIncludeRegistered("js_DV"))
{
Page.ClientScript.RegisterClientScriptInclude("js_DV", scriptUrl);
}
// Determines whether the validation control can be rendered
// for a newer ("uplevel") browser.
// check if client-side validation is enabled.
//evaluationfunction will be null if in PartialRendering, so we re-hash this below
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
if (this.EvaluationFunction == null)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "validationCheck");
if (!String.IsNullOrEmpty(this.ComparisonControlToValidate))
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "comparisonControlID", this.ComparisonControlToValidate);
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "comparisonOperator", this.ComparisonOperator.ToString());
}
if (this.MinDate != new DateTime())
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "minDate", String.Format("{0:MM/dd/yyyy}", this.MinDate));
}
if (this.MaxDate != new DateTime())
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "maxDate", String.Format("{0:MM/dd/yyyy}", this.MaxDate));
}
if (this.UseDefaultErrorMessages)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "useDefaultError", this.UseDefaultErrorMessages.ToString());
}
}
else
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", this.EvaluationFunction);
}
if (this.IsRequired)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID, "isRequired", "true");
}
}
//register our script during asynchronous postback
//this code will just do nothing if there is no ASP.NET AJAX installed
if (Ajax.IsControlInsideUpdatePanel(this) && Ajax.IsInAsyncPostBack(Page) || this.IsInAjaxUpdate)
{
if (!Ajax.IsClientScriptIncludeRegistered(Page, "js_DV"))
{
Ajax.RegisterClientScriptInclude(Page, this.GetType(), "js_DV", scriptUrl);
}
if (this.EvaluationFunction == null)
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "evaluationfunction", "validationCheck");
if (!String.IsNullOrEmpty(this.ComparisonControlToValidate))
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "comparisonControlID", this.ComparisonControlToValidate);
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "comparisonOperator", this.ComparisonOperator.ToString());
}
if (this.MinDate != new DateTime())
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "minDate", String.Format("{0:MM/dd/yyyy}", this.MinDate));
}
if (this.MaxDate != new DateTime())
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "maxDate", String.Format("{0:MM/dd/yyyy}", this.MaxDate));
}
}
else
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "evaluationfunction", this.EvaluationFunction);
}
if (this.IsRequired)
{
Ajax.RegisterExpandoAttribute(Page, this.ClientID, "isRequired", "true");
}
}
base.OnPreRender(e);
}
}
If this post helped you, Mark As Answer.