ASP.NET 自定义服务器控件构建 - 验证器
我正在编写一个 DateValidator 控件来充当所有类型的日期验证器。
它将用作客户端日期比较的必需验证器、最小/最大验证器和范围验证器。
总而言之,我已经编写了它并且它可以运行:在 ASPX 页面中。
由于某种原因,当将其添加到 ASCX 用户控件以用作模板时,它不会呈现所需的 标记,因此在检查以确保在验证继续之前存在控制。
我的类继承了 BaseValidator,因此不会覆盖 RenderChildControls,因为我不需要修改输出。为了添加自定义 JavaScript,我使用 OnPreRender
方法将 RegisterExpandoAttributes
注册到控件,但这无法在 ASCX 中触发,因为控件(span 标记)永远不会呈现。
有没有人遇到过这个问题,如果有,你做了什么来解决它?
编辑:
好吧,又过了 3 个小时,我注意到缺少了一行非常重要的内容: 当在 ASPX 页面内运行时,这会出现在 HTML 中:
<script type="text/javascript">
//<![CDATA[
var Page_ValidationSummaries = new Array(document.getElementById("cal"));
var Page_Validators = new Array(document.getElementById("valTest"));
//]]>
</script>
但是当在 ASPX 中的 ASCX 用户控件中时:
<script type="text/javascript">
//<![CDATA[
var Page_ValidationSummaries = new Array(document.getElementById("uc1_cal"));
//]]>
</script>
如您所见,只有验证摘要被识别,而不是从 BaseValidator 继承的 CustomValidator 类。如果我能找出原因就可以解决问题。令人沮丧。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您应该考虑使用Excentrics World 的免费日历弹出窗口。我认为它具有您描述的所有功能,并且已经构建完成。
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.
如果您使用模板,请调用 InstantiateIn() 方法...
If you're using a template, call the InstantiateIn() method....