跟进我之前关于自定义控件和验证的问题,我现在可以进行服务器端验证,但似乎无法弄清楚如何添加客户端验证。我必须遵循控制:
[ValidationProperty("Text")]
[ToolboxData("<{0}:DateSelect runat=server></{0}:DateSelect>")]
public class DateSelect : System.Web.UI.WebControls.Panel
{
 private DropDownList day;
 private DropDownList month;
 private DropDownList year;
 public DateSelect()
 {
    day = new DropDownList();
    /* some code to create items for 'day' here */
    Controls.Add(day);
    month = new DropDownList();
    /* some code to create items for 'month' here */
    Controls.Add(month);
    year = new DropDownList();
    /* some code to create items for 'year' here */
    Controls.Add(year);
 }
 public string Text
 {
    get
    {
        return year.Text + "-" + month.Text + "-" + day.Text;
    }
 }
}
在我的网络表单上,我添加了这个控件,在 DataType 操作中添加了一个 CompareValidator 来检查有效日期。我将验证器设置EnableClientScript为,false并且自定义控件在服务器端得到了很好的验证,并在正确的情况下提供了正确的消息。但是,当我转向EnableClientScript自定义控件true时,客户端已验证并在 DropDownLists 中实际上存在有效日期时产生错误消息。我一直在试图找出原因并最终得到以下由 .net 生成的 javascript,我认为它永远不会产生正确的值:
function ValidatorGetValueRecursive(control)
{
    if (typeof(control.value) == "string" && (control.type != "radio" || control.checked == true)) {
        return control.value;
    }
    var i, val;
    for (i = 0; i<control.childNodes.length; i++) {
        val = ValidatorGetValueRecursive(control.childNodes[i]);
        if (val != "") return val;
    }
    return "";
}
所以我相信我必须在我的自定义控件中添加一些东西,也许是一段 javascript,一旦验证器尝试验证我的控件并生成正确的值以从三个 DropDownLists 的选定项目中进行验证,它就会被调用。我只是不知道在哪里添加这个脚本。任何指针?