验证 - Web用户控件 [英] Validate - Web User Control

查看:123
本文介绍了验证 - Web用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢用从.NET日历,所以我想有一个Web用户控件与3下拉框,日,月,年。 [code DONE]

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].

我希望能够把这种控制,并与今年开始和结束一年初始化,并且有或没有选定的日期。 [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].

此控件将看看是否有选择了一个有效的日期,返回布尔的 [code DONE]

This control will see if there is one valid date selected and return bool [CODE DONE].

然后在我的网页,我希望能够看到如果Web用户的控制是有效的,在某种程度上,我可以正常的.NET验证(准一个必需的字段)使用,问题是,我不知道在哪里把这个code和检索它的网页上的验证控件。 [code未完成]

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?

推荐答案

有两个步骤的验证框架整合您的自定义服务器控件。

There are two steps to integrating your custom server controls with the validation framework.

(1)服务器端:你需要一个ValidationPropertyAttribute添加到您的类,因此验证framwework知道要看验证的时候是什么:

(1) Server side: you'll need to add a ValidationPropertyAttribute to your class, so the validation framwework knows what to look at when validating:

[ValidationProperty("SelectedDate")]
public class MyDateControl : WebControl
{
    public DateTime? SelectedDate { get { ... } set { ... } }
}

(2)与客户端验证挂钩,您必须确保有一个与你的控制相关的输入标签。这样做的一个方法是渲染<输入类型=隐藏>作为Web控件的HTML的第一个子标签。验证框架将拿起这一点。剩下的事情在这里做的,是通过JavaScript每次都设置这个隐藏字段您的一滴起伏变化。

(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.

此方式,您可以与现有的验证控件配合。如果你想不同的方式来验证,你应该看看一个的CustomValidator。

This way, you can tie in with the existing validation controls. If you want different way to validate, you should look at a CustomValidator.

这篇关于验证 - Web用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆