Custom Validator not validating correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GCLEAR
    New Member
    • Mar 2009
    • 2

    Custom Validator not validating correctly

    In the following code I have a dropdownlist and text box, what I'm trying to achieve is when an item from the dropdownlist is selected it needs to make sure the textbox is not null before page IsValid, if not Page is not valid, what I'm getting is Page is valid is textbox is null and not valid is textbox if populated!

    Help, I'm new to this and cannot figure out why this is happening


    Code:
    <%@ Page Language="C#" %>
    
    
    
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <form id="Form1" runat="server">
    
    <p>
    
    Outcome:
    
    <asp:DropDownList ID="editStatusTextBox" runat="server" 
    
    CausesValidation="True" >
    
    <asp:ListItem Selected="true" Value="Call Attempt 1" Text="Call Attempt 1"/>
    
    <asp:ListItem Value="Call Attempt 2" Text="Call Attempt 2"/>
    
    <asp:ListItem Value="Contacted - Interested" Text="Contacted - Interested"/>
    
    <asp:ListItem Value="Contacted - Not Interested" Text="Contacted - Not interested"/>
    
    <asp:ListItem Value="Callback Required" Text="Callback Required"/>
    
    <asp:ListItem Value="Not in Business" Text="Not in Business"/>
    
    <asp:ListItem Value="Number not in use" Text="Number not in use"/>
    
    <asp:ListItem Value="Using alternative product" Text="Using alternative product"/>
    
    <asp:ListItem Value="Nurture" Text="Nurture"/>
    
    </asp:DropDownList>
    
    Date: 
    
    <asp:TextBox id="editCallBack" 
    
    runat="server"></asp:TextBox>
    
    <script runat="server">
    
    void Button1_Click(Object sender, EventArgs e)
    
    {
    
    if (Page.IsValid)
    
    {
    
    Label1.Text = "Page is Valid";
    
    }
    
    else
    
    {
    
    Label1.Text = "Page Not Valid";
    
    }
    
    }
    
    
    
    
    
        void ValidateNumber(object source, ServerValidateEventArgs args)
    
            {
    
    
                try
    
                    {
    
    
                    if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 1")
    
                        {
    
                         args.IsValid = false;
    
                        }
    
                    else if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 2")
    
                        {
    
                        args.IsValid = false;
    
                        }
    
                    }
    
                catch (Exception ex)
    
                    {
    
                    args.IsValid = false;
    
                    }
    
             }
    
    </script>
    
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="editCallBack" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>
    
    &nbsp;
    
    <asp:CustomValidator id="CustomValidator1" 
    
    runat="server" ControlToValidate="editCallBack" 
    
    ErrorMessage="You must enter a Date" 
    
    OnServerValidate="ValidateNumber"></asp:CustomValidator>
    
    </p>
    
    <p>
    
    <asp:Button id="Button1" onclick="Button1_Click" 
    
    runat="server" Text="Button"></asp:Button>
    
    </p>
    
    <p>
    
    <asp:Label id="Label1" runat="server"></asp:Label>
    
    </p>
    
    </form>
    
    </body>
    
    </html>
    Last edited by Frinavale; Mar 16 '09, 03:31 PM. Reason: Added [code] tags: Plase post code in [code] [/code] tags.
  • GCLEAR
    New Member
    • Mar 2009
    • 2

    #2
    sorry this is the code I'm using
    Code:
    <%@ Page Language="C#" %>
    
    <script runat="server">
    
    void Button1_Click(Object sender, EventArgs e)
    
    {
    
    if (Page.IsValid)
    
    {
    
    Label1.Text = "Page is Valid";
    
    }
    
    else
    
    {
    
    Label1.Text = "Page Not Valid";
    
    }
    
    }
    
    
    
    
    
        void ValidateNumber(object source, ServerValidateEventArgs args)
    
            {
    
    
                try
    
                    {
    
    
                    if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 1")
    
                        {
    
                         args.IsValid = false;
    
                        }
    
                    else if ((Eval(editCallBack.Text) == null) && this.editStatusTextBox.Text == "Call Attempt 2")
    
                        {
    
                        args.IsValid = false;
    
                        }
    
                    }
    
                catch (Exception ex)
    
                    {
    
                    args.IsValid = false;
    
                    }
    
             }
    
    </script>
    
    <html>
    
    <head>
    
    </head>
    
    <body>
    
    <form id="Form1" runat="server">
    
    <p>
    
    Outcome:
    
    <asp:DropDownList ID="editStatusTextBox" runat="server" 
    
    CausesValidation="True" >
    
    <asp:ListItem Selected="true" Value="Call Attempt 1" Text="Call Attempt 1"/>
    
    <asp:ListItem Value="Call Attempt 2" Text="Call Attempt 2"/>
    
    <asp:ListItem Value="Contacted - Interested" Text="Contacted - Interested"/>
    
    <asp:ListItem Value="Contacted - Not Interested" Text="Contacted - Not interested"/>
    
    <asp:ListItem Value="Callback Required" Text="Callback Required"/>
    
    <asp:ListItem Value="Not in Business" Text="Not in Business"/>
    
    <asp:ListItem Value="Number not in use" Text="Number not in use"/>
    
    <asp:ListItem Value="Using alternative product" Text="Using alternative product"/>
    
    <asp:ListItem Value="Nurture" Text="Nurture"/>
    
    </asp:DropDownList>
    
    Date: 
    
    <asp:TextBox id="editCallBack" 
    
    runat="server"></asp:TextBox>
    
    
    
    
    
    <asp:CustomValidator id="CustomValidator1" 
    
    runat="server" ControlToValidate="editCallBack" 
    
    ErrorMessage="You must enter a Date" 
    
    OnServerValidate="ValidateNumber"></asp:CustomValidator>
    
    </p>
    
    <p>
    
    <asp:Button id="Button1" onclick="Button1_Click" 
    
    runat="server" Text="Button"></asp:Button>
    
    </p>
    
    <p>
    
    <asp:Label id="Label1" runat="server"></asp:Label>
    
    </p>
    
    </form>
    
    </body>
    
    </html>
    Last edited by Frinavale; Mar 16 '09, 03:31 PM. Reason: Added [code] tags. Please post code in [code] [/code] tags

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I don't have a lot of experience using the Page.IsValid property, but, you have to call the Page.Validate() method before you can access it or else it wont be set.

      Please see what MSDN has on the Page.IsValid property.

      -Frinny

      Comment

      Working...