Display Property Validation Controls?

Last post 10-15-2006 1:57 AM by RN5A. 7 replies.

Sort Posts:

  • Confused [8-)] Display Property Validation Controls?

    10-09-2006, 9:56 PM
    • Participant
      961 point Participant
    • RN5A
    • Member since 07-13-2006, 3:59 PM
    • Mumbai, India
    • Posts 406
    The different Validation controls like RequiredFieldValidator, RangeValidator etc. have a property named Display. This property can have 3 values - Dynamic, Static & None. What's the difference between the Dynamic Display & Static Display?

    Also suppose a Form has 2 TextBoxes & both the TextBoxes have the RequiredFieldValidator. The Form has a Button as well.

    <form runat="server">
    First Name: <asp:TextBox ID="txtFName" runat="server"/>
    <asp:RequiredFieldValidator ID="reqFName" ControlToValidate="txtFName" Display="static" ErrorMessage="Enter Your First Name" runat="server"/><br>
    Last Name: <asp:TextBox ID="txtLName" runat="server"/>
    <asp:RequiredFieldValidator ID="reqLName" ControlToValidate="txtLName" Display="static" ErrorMessage="Enter Your Last Name" runat="server"/><br>
    <asp:Button ID="btn" OnClick="SubmitClick" Text="SUBMIT" runat="server"/>
    </form>


    Assume that the cursor is in the txtFName TextBox i.e. txtFName has the focus. Without typing anything in the txtFName TextBox, I set the focus to the txtLName TextBox. When I do this, the RequiredFieldValidator gets into the act & immediately displays the ErrorMessage since the txtFName TextBox is empty.

    Now how do I ensure that the RequiredFieldValidator gets into the act & displays the ErrorMessage (if any or both the TextBoxes are empty) only when the user clicks the Submit Button?

    I don't want the RequiredFieldValidator to display the ErrorMessage as soon as an empty TextBox loses focus; rather the ErrorMessage should be displayed only when the Submit Button is clicked & ASP.NET finds that one (or any) of the TextBoxes is empty.
    Thanks

    RON
    *********
    If you have an apple & I have an apple & we exchange our apples, then each of us will still have only 1 apple but if you have an idea & I have an idea & we exchange our ideas, then each of us will have 2 ideas
  • Re: Display Property Validation Controls?

    10-09-2006, 11:04 PM
    • All-Star
      23,295 point All-Star
    • douglas.reilly
    • Member since 11-19-2002, 9:19 PM
    • Brick, NJ USA
    • Posts 4,647

    Static means that even when not visible, the space that will be used by the error message will be taken on the screen (but blank) even when the validator is not invalid.  Dynamic (generally a good setting) means that the validator will not take space on the page until the validator determins the control validated is invalid.

     To do what you wish with the display of error message, set the EnableClientScript property of the validator to false.

    Starting with ASP.NET 2.0? Look at:
    Programming Microsoft Web Forms
    My Blog
  • Re: Display Property Validation Controls?

    10-09-2006, 11:27 PM
    Answer
    • All-Star
      30,305 point All-Star
    • PLBlum
    • Member since 06-28-2002, 9:20 AM
    • Boston, MA
    • Posts 5,344
    • TrustedFriends-MVPs

    The RequiredFieldValidator does not display an error when focus leaves the control unless the user changed the text. All validators fire during the DHTML onchange event. That event requires a user edit and the text that was in the control when focus was applied is different from when focus is lost.

    For the RequiredFieldValidator, that means the textbox must already have text when focus is applied and the user must remove all of that text prior to removing the focus.

    --- Peter Blum
    Creator of Peter's Data Entry Suite (formerly Professional Validation And More and Peter's Date Package) and Peter's Polling Package
    www.PeterBlum.com
  • Re: Display Property Validation Controls?

    10-13-2006, 12:45 PM
    • Participant
      961 point Participant
    • RN5A
    • Member since 07-13-2006, 3:59 PM
    • Mumbai, India
    • Posts 406
    No, my dear friend Douglas, setting EnableClientScript to False doesn't resolve the issue. Leave alone suppressng the ErrorMessage when a user moves from one control to another (assuming that the first control doesn't have valid data), the Validation Controls don't even fire when the Form is submitted. In other words, the Form gets submitted without any validation just because of the presence of EnableClientScript=False in the validator!

    Please try it out on your m/c. & let me know whether you experience the same result or not.

    Any other ideas???
    Thanks

    RON
    *********
    If you have an apple & I have an apple & we exchange our apples, then each of us will still have only 1 apple but if you have an idea & I have an idea & we exchange our ideas, then each of us will have 2 ideas
  • Re: Display Property Validation Controls?

    10-13-2006, 8:33 PM
    • All-Star
      23,295 point All-Star
    • douglas.reilly
    • Member since 11-19-2002, 9:19 PM
    • Brick, NJ USA
    • Posts 4,647
    check IsValid on server side page_load or button click event. you should alwas check tha anyway.
    Starting with ASP.NET 2.0? Look at:
    Programming Microsoft Web Forms
    My Blog
  • Re: Display Property Validation Controls?

    10-14-2006, 5:25 PM
    • Participant
      961 point Participant
    • RN5A
    • Member since 07-13-2006, 3:59 PM
    • Mumbai, India
    • Posts 406
    Thanks, my dear Douglas...you are absolutely right. Disabling EnableClientScript in conjunction with Page.IsValid does suppress the ErrorMessage when a control, which has invalid data, loses focus but this gives rise to another petty problem.

    Suppose a Form has a TextBox & a Button. I want users to enter their age in the TextBox. It's quite obvious that this TextBox should only accept numbers & no letters. So along with a RequiredFieldValidator, the TextBox also has a RegularExpressionValidator which ensures that users enter only numbers. Note that the sub that the Button invokes makes use of Page.IsValid.

    Suppose a user enters some letters in the TextBox instead of a number. Now since EnableClientScript is set to False, when the TextBox loses focus, the ErrorMessage gets suppressed. The user clicks the Button. The ErrorMessage gets displayed now since the Button sub uses Page.IsValid. No problems till here....

    Assume that after this, the user enters his proper age, say, 25, in the TextBox which is very much valid. But now though the data in the TextBox is valid, when the TextBox loses focus, the ErrorMessage still remains. I want the ErrorMessage to get suppressed under such circumstances since the data in the TextBox is valid now. How do I accomplish this?
    Thanks

    RON
    *********
    If you have an apple & I have an apple & we exchange our apples, then each of us will still have only 1 apple but if you have an idea & I have an idea & we exchange our ideas, then each of us will have 2 ideas
  • Re: Display Property Validation Controls?

    10-14-2006, 6:29 PM
    • All-Star
      23,295 point All-Star
    • douglas.reilly
    • Member since 11-19-2002, 9:19 PM
    • Brick, NJ USA
    • Posts 4,647
    I cannot think if a way to do that, save whipping up your own control that always emits Javascript that only clears the error message when valid data is entered...
    Starting with ASP.NET 2.0? Look at:
    Programming Microsoft Web Forms
    My Blog
  • Re: Display Property Validation Controls?

    10-15-2006, 1:57 AM
    • Participant
      961 point Participant
    • RN5A
    • Member since 07-13-2006, 3:59 PM
    • Mumbai, India
    • Posts 406
    Thanks for the response, Douglas!!
    Thanks

    RON
    *********
    If you have an apple & I have an apple & we exchange our apples, then each of us will still have only 1 apple but if you have an idea & I have an idea & we exchange our ideas, then each of us will have 2 ideas
Page 1 of 1 (8 items)