验证 - Web 用户控制
我不喜欢使用 .NET 中的日历,所以我想要一个带有 3 个下拉框(日、月、年)的 Web 用户控件。 [代码完成]。
我希望能够调用此控件并使用开始年份和结束年份以及选择或不选择日期来初始化它。[代码完成]。
该控件将查看是否选择了一个有效日期并返回布尔值[CODE DONE]。
然后在我的网页中,我希望能够以一种可以与正常 .NET 验证一起使用的方式查看该 Web 用户控件是否有效(关联一个必填字段),问题是我不知道在哪里将此代码放入网页上的验证控件中并将其检索。 [代码未完成]。
我该怎么做?
I do not like to use the calendar from .NET, so I would like to have one Web User Control with 3 drop down boxes, day, month, year. [CODE DONE].
I want to be able to call this Control and initialize it with start year and end year, and with or without selected date.[CODE DONE].
This control will see if there is one valid date selected and return bool [CODE DONE].
Then in my web page I would like to able to see if that web user control is valid, in a way that I can use with the normal .NET validation (associate one required field), the problem is that I don't know where to put this code and retrieve it to the validation control on the web page. [CODE NOT DONE].
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用 CustomValidator对此进行控制。请参阅本教程 这解释了如何使用客户端和服务器端版本的验证来实现它。
You want to use the CustomValidator control for this. See this tutorial that explains how to implement it with both a client-side and server-side version of the validation.
将自定义服务器控件与验证框架集成有两个步骤。
(1) 服务器端:您需要向您的类添加 ValidationPropertyAttribute,以便验证框架知道验证时要查看的内容:
(2) 要连接客户端验证,您必须确保有一个输入标记与您的控制相关。一种方法是渲染一个 作为 Web 控件 HTML 的第一个子标记。验证框架将对此进行处理。这里剩下要做的一件事是,每次下拉菜单发生变化时,通过 JavaScript 设置此隐藏字段。
这样,您就可以与现有的验证控件结合起来。如果您想要不同的验证方式,您应该查看 CustomValidator。
There are two steps to integrating your custom server controls with the validation framework.
(1) Server side: you'll need to add a ValidationPropertyAttribute to your class, so the validation framwework knows what to look at when validating:
(2) To hook up with client side validation, you have to make sure there's an input tag associated with your control. One way of doing that is rendering an <input type="hidden"> as the first child tag of your web control's HTML. The validation framework will pick up on that. The remaining thing to do here, is to set this hidden field through JavaScript each time your one drop downs changes.
This way, you can tie in with the existing validation controls. If you want different way to validate, you should look at a CustomValidator.