我有一個自定義客戶端驗證的用戶控件。當我在頁面上擁有用戶控件時,它沒有任何問題。但是,如果該頁面有回發事件(從下拉列表中選擇某個選項,並顯示更多字段),我的驗證不再有效。我的驗證如下:UpdatePanel和RegisterStartupScript之間的衝突?
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>
任何原因,特別是這個JavaScript不能直接添加到標記或通過腳本標籤從文件添加? – MilkyWayJoe
Javascript處於用戶控制中,因此並不總是必需的。我曾嘗試將Javascript放入我的js文件中,但它沒有任何區別。看起來問題在於,一旦UpdatePanel觸發,原始DOM就會被刪除。 – MetalAdam
〜這個驗證腳本觸發了什麼?另外,如果您不使用jQuery,則可能需要查看['add_endRequest'](http://msdn.microsoft.com/zh-cn/library/bb383810.aspx)。您可以使用此處理程序重新綁定您的驗證程序,無論它是調用您的函數 – MilkyWayJoe