部分页面更新后,Asp.net字段验证器未触发

时间:2011-06-13 13:20:17

标签: asp.net asp.net-ajax

我在Asp.net UpdatePanel中有TextBox,RequiredFieldValidator和一个Button。最初,RequiredFieldValidator的Enabled属性设置为false,以便在UpdatePanel内单击Button时,验证不会首次触发。但是在UpdatePanel内的Button的事件处理程序中,我将TextBox的值设置为DateTime.Now,我也使用ScriptManager.RegisterStartupScript方法注册启动脚本,通过调用ValidatorEnable函数在客户端上启用上述验证器。如果TextBox为空,我还会在ClickPanel上面有一个按钮,其中必须触发RequiredFieldValidator。但是,在单击UpdatePanel内的Button后,TextBox的值将设置为最新日期时间并启用验证程序,但在从文本框中删除文本并单击updatepanel外部的按钮后,不会触发验证程序。

请参阅以下代码。

客户端代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtSomeValue" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="rqdtxtSomeValue" runat="server" Enabled="false" ErrorMessage="*"
            ControlToValidate="txtSomeValue"></asp:RequiredFieldValidator>
        <asp:Button ID="btnGetValue" runat="server" Text="Get Current Date" OnClick="btnGetValue_Click" />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="btnSave" runat="server" Text="Save" />

服务器端代码:

protected void btnGetValue_Click(object sender, EventArgs e)
{
    txtSomeValue.Text = DateTime.Now.ToString();

    string script = @"javascript:ValidatorEnable(document.getElementById('" + rqdtxtSomeValue.ClientID + "', true))";
    ScriptManager.RegisterStartupScript(btnGetValue, btnSave.GetType(), "btnGetValue_Click", script, true);
}

有人可以告诉我如何在部分页面更新后重新启用字段验证器吗?

1 个答案:

答案 0 :(得分:0)

您确定在btnGetValue返回并更新UpdatePanel后是否启用了验证器?你是如何测试它是否启用了验证器?您是否尝试过在代码隐藏中启用验证器?

protected void btnGetValue_Click(object sender, EventArgs e) {
    txtSomeValue.Text = DateTime.Now.ToString();

    rqdtxtSomeValue.Enabled = true;
    ScriptManager.RegisterStartupScript(btnGetValue, btnSave.GetType(), "btnGetValue_Click", script, true); 
}