开发者

How to make RequiredFieldValidator disappear and then appear?

I want the required field validator to become only visible whenever there is an error..not all the time.. How can i achieve so?

<asp:RequiredFieldValidator
    runat="server"
    ControlToValidate="Editor1"
    ErrorMessage="fffffff"
    ForeColor="#CC0000"
    Display="Dynamic" /开发者_如何学运维>

The error message appears as soon as I run the page..I want it to appear whenever there is an error..When the field is empty!!


The only cause of your problem in your current page is that, in the page Load event, somewhere Page.Validate() is called and you need to to check that.

I have tested this sample at my end and the validator is not fired to me at page load.

Can you try this in a separate new Page?

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="fffffff"
ForeColor="#CC0000"
Display="Dynamic" />
    <asp:Button ID="Button1" runat="server" Text="Button" />

If you check the above mentioned example, it will not fire Validator on page load, but you can add this code, and you will find, the validator is fired on page load..

protected void Page_Load(object sender, EventArgs e)
{
    Page.Validate();
}

Please note, I have tested both scenarios on the new page, just try it and you will understand the problem.


If you think about it, at page load the field is empty...and therefore an error. Give the requiredfieldvalidator the property of InitialValue="" and that should give you what you want.


Set Display Property of required field validator to Dynamic

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜