开发者

Asp.net field validator not firing after partial page update

I have TextBox, RequiredFieldValidator and a Button inside the Asp.net UpdatePanel. Initially the the Enabled property of the RequiredFieldValidator is set to false so that on the click of Button inside the UpdatePanel the validation does not fire for the first time. However on the event handler of the Button inside the UpdatePanel I am setting the value of TextBox to DateTime.Now, also I am registering a startup script using ScriptManager.RegisterStartupScript method to enable the above mentioned validator on the client by calling the ValidatorEnable function. I also have a Button outside UpdatePanel on the click of which the RequiredFieldValidator should fire if the TextBox is empty. However after clicking the Button inside the UpdatePanel the value of the TextBox is set to latest date time and the validator is enabled, but after removing the text from the textbox and clicking on the button outside the updatepanel the validator is not fired.

Please refer to following code.

Client Side code:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="txtSomeValue" runat="server">开发者_Go百科;</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" />

Server Side Code:

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);
}

Can anybody tell me how to re-enable the the field validators after partial page update?


Are you sure the validator is being enabled after btnGetValue returns and updates the UpdatePanel? How are you testing that it is enabling the validator? Have you tried setting the validator to enabled in your code-behind?

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); 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜