views:

197

answers:

2

I am writing a DateValidator control to function as a validator of all type for dates.

It will function as an Required validator, a Min/Max validator, and a Range Validator for date comparisons on the client side.

All in all, I have it written and it functions: In an ASPX page.

For some reason, when adding it into an ASCX User Control to be used as a template, it does not render the required <span> tag and thus generates a JavaScript null reference when checking to make sure the control exists before validation continues.

My class inherits BaseValidator and thus does not override RenderChildControls since I don't need to modify the output. To add my custom javascript, I use the OnPreRender method to RegisterExpandoAttributes to the control, but this cannot fire in an ASCX since the control (the span tag) is never rendered.

Has anyone encountered this and if so, what did you do to fix it?

EDIT:

Ok, after another 3 hours, I've noticed a very important line missing: When run inside an ASPX page, this appears in the HTML:

<script type="text/javascript">
//<![CDATA[
var Page_ValidationSummaries =  new Array(document.getElementById("cal"));
var Page_Validators =  new Array(document.getElementById("valTest"));
//]]>
</script>

But when in an ASCX User Control in an ASPX:

<script type="text/javascript">
//<![CDATA[
var Page_ValidationSummaries =  new Array(document.getElementById("uc1_cal"));
//]]>
</script>

As you can see, only the Validation Summary is being recognized and not my CustomValidator class inheriting from BaseValidator. If I can iron out why it will solve the problem. Frustrating.

A: 

If you're using a template, call the InstantiateIn() method....

IrishChieftain
The ASCX control is being used as a template that can be re-used over and over. It does not implement ITemplate.
Jonathan
+1  A: 

Maybe you should consider using Excentrics World's free calendar popup. I think it has all the functionality you described and it has already been built.

Clark
I'm building a complete set of controls for my company. This is only one of about 50 others. I'm building them to save money for the company, and to ensure they're open source, fully documented, and relatively easy to modify.
Jonathan