RequiredValidator not working

Discussion in 'ASP .Net' started by tshad, May 20, 2005.

  1. tshad

    tshad Guest

    I have the following:

    <asp:textbox id="ProfileName" columns="45" runat="server" />
    <asp:RequiredFieldValidator
    ControlToValidate="ProfileName"
    Display="Dynamic"
    Text="Name Required"
    runat="server" />

    I also have an imagebutton that goes to a routine to save a record

    <asp:ImageButton ID="SaveRecord" runat="server" Visible="true"
    OnClick="ModifyRecord_Click" ImageUrl="../../buttons/SaveChanges_0.gif" />

    I put nothing in the ProfileName field and it still goes to the function
    ModifyRecord_Click instead of displaying the "Name Required" message.

    Why is it not working?

    Thanks,

    Tom
    tshad, May 20, 2005
    #1
    1. Advertising

  2. tshad

    TJS Guest

    I have a similar problem and wonder if this is like mine.

    Are you having this problem in both IE and Firefox browsers ?
    Are you prepopulating the field for the user to show the current value from
    a datastore ?
    TJS, May 20, 2005
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "TJS" <> wrote in message
    news:...
    >I have a similar problem and wonder if this is like mine.
    >
    > Are you having this problem in both IE and Firefox browsers ?
    > Are you prepopulating the field for the user to show the current value
    > from a datastore ?


    Actually,

    It does it with both.

    But I also was getting an error before in the function the button was
    calling, so I assume the validator wasn't being called.

    I assumed that because I got the error, the validator didn't work.

    What is happening is that the function is being called after the Validator
    has run?

    I thought if there was an error, the function wouldn't be called. Am I
    wrong here?

    Thanks,

    Tom
    tshad, May 20, 2005
    #3
  4. tshad

    TJS Guest

    You're right, the function should not be called if the page is not valid

    since it fails in both browsers, and you aren't prefilling the form, I would
    need to see your code for "ModifyRecord_Click" to determine of the problem
    is there.
    TJS, May 20, 2005
    #4
  5. tshad

    tshad Guest

    "TJS" <> wrote in message
    news:...
    > You're right, the function should not be called if the page is not valid
    >
    > since it fails in both browsers, and you aren't prefilling the form, I
    > would need to see your code for "ModifyRecord_Click" to determine of the
    > problem is there.
    >


    Actually, what I did to make it work is:

    Sub ModifyRecord_Click(sendor as Object, e as ImageClickEventArgs)

    if not Page.isValid then
    exit Sub
    End If

    I guessed the function does get called, so I just added that code to solve
    the problem.

    Tom
    tshad, May 20, 2005
    #5
  6. tshad

    tshad Guest

    "tshad" <> wrote in message
    news:...
    > "TJS" <> wrote in message
    > news:...
    >> You're right, the function should not be called if the page is not valid
    >>
    >> since it fails in both browsers, and you aren't prefilling the form, I
    >> would need to see your code for "ModifyRecord_Click" to determine of the
    >> problem is there.
    >>

    >
    > Actually, what I did to make it work is:
    >
    > Sub ModifyRecord_Click(sendor as Object, e as ImageClickEventArgs)
    >
    > if not Page.isValid then
    > exit Sub
    > End If
    >
    > I guessed the function does get called, so I just added that code to solve
    > the problem.
    >
    > Tom
    >
    tshad, May 20, 2005
    #6
  7. tshad

    tshad Guest

    "tshad" <> wrote in message
    news:...
    > "TJS" <> wrote in message
    > news:...
    >> You're right, the function should not be called if the page is not valid
    >>
    >> since it fails in both browsers, and you aren't prefilling the form, I
    >> would need to see your code for "ModifyRecord_Click" to determine of the
    >> problem is there.
    >>

    >
    > Actually, what I did to make it work is:
    >
    > Sub ModifyRecord_Click(sendor as Object, e as ImageClickEventArgs)
    >
    > if not Page.isValid then
    > exit Sub
    > End If
    >
    > I guessed the function does get called, so I just added that code to solve
    > the problem.
    >


    Now the problem is in my other routine, which is called by my LinkButton
    (the Modify routine was called by an ImageButton).

    The code is the same:

    Sub DeleteProfile_Click(sender as Object, e as EventArgs)
    trace.warn("inside SelectProfile")

    if not Page.isValid then
    exit Sub
    End If

    But when I do it I get the error:

    Page.IsValid cannot be called before validation has taken place. It should
    be queried in the event handler for a control with CausesValidation=True or
    after a call to Page.Validate.

    So in the Modify function, the validate function is being called and in the
    Delete function it is not ???????

    Tom
    > Tom
    >
    tshad, May 20, 2005
    #7
  8. tshad

    Peter Blum Guest

    The method Page.Validate() is called automatically by each of Microsoft's
    submit controls (Button, ImageButton, and LinkButton) unless you set
    CausesValidation to false. If its not called, you cannot use Page.IsValid as
    you've found.

    The submit controls call it in their OnClick method (which then fires your
    Click event handler). It runs after Page_Load.
    - Make sure CausesValidation=true or you call Page.Validate() before calling
    Page.IsValid
    - Don't call any of this from within Page_Load. Let it happen in the post
    back event methods.
    - If you are using CausesValidation=false because you don't want to validate
    every validator, call individual validator's Validate() methods. Then check
    the IsValid property on those individual validators.
    - This is all server side code. It should not be affected by the browser
    type.
    - It is possible that if javascript is not working, your Click post back
    event will not run. This happens when your submit control calls
    __doPostBack() on the client-side to set itself up for server side
    processing. Without javascript running, __doPostBack() never runs.

    --- Peter Blum
    www.PeterBlum.com
    Email:
    Creator of "Professional Validation And More" at
    http://www.peterblum.com/vam/home.aspx

    "tshad" <> wrote in message
    news:OFBy%...
    > "tshad" <> wrote in message
    > news:...
    >> "TJS" <> wrote in message
    >> news:...
    >>> You're right, the function should not be called if the page is not valid
    >>>
    >>> since it fails in both browsers, and you aren't prefilling the form, I
    >>> would need to see your code for "ModifyRecord_Click" to determine of the
    >>> problem is there.
    >>>

    >>
    >> Actually, what I did to make it work is:
    >>
    >> Sub ModifyRecord_Click(sendor as Object, e as ImageClickEventArgs)
    >>
    >> if not Page.isValid then
    >> exit Sub
    >> End If
    >>
    >> I guessed the function does get called, so I just added that code to
    >> solve the problem.
    >>

    >
    > Now the problem is in my other routine, which is called by my LinkButton
    > (the Modify routine was called by an ImageButton).
    >
    > The code is the same:
    >
    > Sub DeleteProfile_Click(sender as Object, e as EventArgs)
    > trace.warn("inside SelectProfile")
    >
    > if not Page.isValid then
    > exit Sub
    > End If
    >
    > But when I do it I get the error:
    >
    > Page.IsValid cannot be called before validation has taken place. It should
    > be queried in the event handler for a control with CausesValidation=True
    > or after a call to Page.Validate.
    >
    > So in the Modify function, the validate function is being called and in
    > the Delete function it is not ???????
    >
    > Tom
    >> Tom
    >>

    >
    >
    Peter Blum, May 21, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SStory

    requiredvalidator for checkbox

    SStory, Jul 30, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    1,339
    SStory
    Jul 30, 2003
  2. Elie Medeiros via .NET 247

    Custom Validator not working without RequiredValidator

    Elie Medeiros via .NET 247, May 18, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,657
    William F. Robertson, Jr.
    May 18, 2005
  3. TJS
    Replies:
    3
    Views:
    546
  4. Shimon Sim
    Replies:
    4
    Views:
    22,131
    Patrick.O.Ige
    Feb 3, 2006
  5. Niraj Sikotara
    Replies:
    2
    Views:
    541
    Niraj Sikotara
    Sep 21, 2006
Loading...

Share This Page