CUSTOMVALIDATOR works but page goes on on FALSE

Discussion in 'ASP .Net' started by Jose Fernandez, Jun 1, 2005.

  1. Hola

    I have this customvalidator

    <asp:CustomValidator id="ComboValidator" runat="server"
    ErrorMessage="CustomValidator" /> (The OnServerValidate method is set in
    Events windows of the Control Property)


    THIS IS THE METHOD
    private void ValidarCombo(object sender, ServerValidateEventArgs e)
    {
    int counter=0;
    foreach(Control control in this.Controls[1].Controls)
    {
    if(control.GetType().ToString().IndexOf("DropDownList")!=-1)
    {
    DropDownList combo=(DropDownList)control;
    if(combo.SelectedItem.Value=="---")
    {
    counter++;
    }
    }
    }
    e.IsValid=(counter==0);
    counter++;
    }

    It works perfectly. If a DropDownList is not selected, e.Valid is set to
    false and also Page.IsValid BUT the page continues as if it was TRUE. Am I
    missing something??
    Thanks in advance
     
    Jose Fernandez, Jun 1, 2005
    #1
    1. Advertising

  2. Jose Fernandez

    Peter Blum Guest

    Two things:

    1. You must *always* test Page.IsValid in your Click event method before
    saving. The Click event method gets called whether IsValid is true or false.
    So its up to you to stop it from running your Save code.

    2. If you are trying to prevent saving a "no selection" state, you can use
    the RequiredFieldValidator. Just set its InitialText property to the value
    of the no selection item. ("---" in your case, I think.)

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

    "Jose Fernandez" <> wrote in message
    news:...
    > Hola
    >
    > I have this customvalidator
    >
    > <asp:CustomValidator id="ComboValidator" runat="server"
    > ErrorMessage="CustomValidator" /> (The OnServerValidate method is set in
    > Events windows of the Control Property)
    >
    >
    > THIS IS THE METHOD
    > private void ValidarCombo(object sender, ServerValidateEventArgs e)
    > {
    > int counter=0;
    > foreach(Control control in this.Controls[1].Controls)
    > {
    > if(control.GetType().ToString().IndexOf("DropDownList")!=-1)
    > {
    > DropDownList combo=(DropDownList)control;
    > if(combo.SelectedItem.Value=="---")
    > {
    > counter++;
    > }
    > }
    > }
    > e.IsValid=(counter==0);
    > counter++;
    > }
    >
    > It works perfectly. If a DropDownList is not selected, e.Valid is set to
    > false and also Page.IsValid BUT the page continues as if it was TRUE. Am I
    > missing something??
    > Thanks in advance
    >
     
    Peter Blum, Jun 2, 2005
    #2
    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. Rajarshi

    0 == False but [] != False?

    Rajarshi, May 24, 2007, in forum: Python
    Replies:
    20
    Views:
    719
    Erik Max Francis
    May 30, 2007
  2. DJ
    Replies:
    3
    Views:
    952
  3. Don
    Replies:
    2
    Views:
    551
  4. zigzagdna
    Replies:
    1
    Views:
    540
    Arne Vajhøj
    Jul 29, 2010
  5. Jose Fernandez

    CUSTOMVALIDATOR works but page goes on on FALSE

    Jose Fernandez, Jun 1, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    139
    Jose Fernandez
    Jun 1, 2005
Loading...

Share This Page