0

我有一个带有一些自定义客户端验证的用户控件。当我在页面上有用户控件时,它没有问题。但是,如果该页面有回发事件(从下拉列表中选择某个选项,并显示更多字段),我的验证似乎不再有效。我的验证如下:

protected void Page_Load(object sender, EventArgs e)
{
    Type cstype = this.GetType();

    if (!Page.ClientScript.IsStartupScriptRegistered(cstype, "ValidatorType"))
    {
        String DateValidator;
        DateValidator = "<script type=\"text/javascript\">\n";
        DateValidator += "function ValidateDate(source, args) {\n";
        DateValidator += "   var ddDay = document.getElementById(source.day);\n";
        DateValidator += "   var day = ddDay.selectedIndex;";
        DateValidator += "   var ddMonth = document.getElementById(source.month);\n";
        DateValidator += "   var month = ddMonth.selectedIndex;\n";
        DateValidator += "   var ddYear = document.getElementById(source.year);\n";
        DateValidator += "   var year = ddYear.selectedIndex;\n";
        DateValidator += "   if (day == 0 || month == 0 || year == 0)\n";
        DateValidator += "      args.IsValid = false;\n";
        DateValidator += "   else\n";
        DateValidator += "      args.IsValid = true;\n";
        DateValidator += "   }\n";
        DateValidator += "</script>";
        Page.ClientScript.RegisterStartupScript(cstype, "ValidatorType", DateValidator);
    }
    Page.ClientScript.RegisterExpandoAttribute(reqDueDate.ClientID, "month", ddMonth.ClientID);
    Page.ClientScript.RegisterExpandoAttribute(reqDueDate.ClientID, "day", ddDay.ClientID);
    Page.ClientScript.RegisterExpandoAttribute(reqDueDate.ClientID, "year", ddYear.ClientID);
}

我难住了。有什么建议吗?

编辑:

这是我正在使用的用户控件。

<asp:DropDownList ID="ddMonth" runat="server" AppendDataBoundItems="true">
   <asp:ListItem Value="">--Month--</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddDay" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="">--Day--</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddYear" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="">--Year--</asp:ListItem>
</asp:DropDownList>
<asp:CustomValidator ID="reqDueDate" EnableClientScript="true" ClientValidationFunction="ValidateDate" runat="server" ErrorMessage="Required" CssClass="input-notification error png_bg" Display="Dynamic"></asp:CustomValidator>
4

2 回答 2

5

事实证明,我需要做两件事。在我的用户代码中放置一个更新面板,然后更改行

Page.ClientScript.RegisterExpandoAttribute(reqDueDate.ClientID, "day", ddDay.ClientID);

ScriptManager.RegisterExpandoAttribute(UpdatePanelDDD, reqDueDate.ClientID, "day", ddDay.ClientID, false);

Thanks all for your help and http://www.aspnetajaxtutorials.com/2010/09/customvalidator-registerexpandoattribut.html for the guidance.

于 2012-07-05T14:19:28.943 回答
0

你可以做类似这样的事情:

<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

var btn = null;

var EndRequestHandler = function(sender, args) {
    btn = document.getElementById('<%= YourButton.ClientID %>');
    btn.onclick = ValidateDate;
};
</script>

或者您可以将其附加到客户端的表单提交事件中

编辑endRequest当 MS Ajax 完成任何 asyc 请求时触发该事件,例如UpdatePanel更新 DOM。您可以在此处找到更多详细信息

于 2012-07-04T18:44:17.050 回答