Solved

Custom Validator Not Working

Posted on 2004-09-30
8
636 Views
Last Modified: 2007-11-27
I have a custom validator on my aspx page that will just not work.  Below is the code in the code behind that should be called when I press the Save button, but it never is.  If I run in debug, this block of code is never even reached.  I have "Page.IsValid" being called in the Save code.  I also have a RequiredValidator on this same page that works fine so the problem is something with this validator.

For the record I have "EnableClientScript" set to false.  However, I even tried setting it to true and that did not work either.

I am missing something small?


CODE BEHIND:
Sub valSchedule_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles valSchedule.ServerValidate

        If Request.QueryString("Edit") <> "yes" Then
            If cboSchedule.SelectedItem.Value = "" Then
                args.IsValid() = False
            End If
        End If

    End Sub
0
Comment
Question by:mattgoolsby
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 2

Expert Comment

by:Arvaris
ID: 12194703
Are you sure the custom validator is Enabled?

or you could manually try calling

valSchedule.Enabled = True
valSchedule.Validate()

in the function before you call If IsValid()

Also, make sure the button you're using doesn't have CausesValidation=False.

BTW - EnableClientScript only works if you have OnClientValidate set to a javascript function, otherwise it is ignored even if true
0
 

Author Comment

by:mattgoolsby
ID: 12195842
The validator is enabled, I tried manually doing the validate, CausesValidation is true.  I have a required field validate that fires fine when I press the button, so I think my issue lies only with the Custom Validator control.
0
 
LVL 2

Expert Comment

by:Arvaris
ID: 12199473
Have you set the required field validator EnableClientScript=False, because if there is a control that fires on client side and one the fires on server side, the client side gets clled first.  and if that fails, the server side validation never gets called.  So if you disable the client side validation on the required field validators, that will make it validate everything server side
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mattgoolsby
ID: 12199494
EnableClientScript is false on both controls
0
 
LVL 2

Expert Comment

by:Arvaris
ID: 12199542
Can you please post more of your code, like that aspx page and codebehind sections that deal with the button click...pretty much anything related to this part of the page.

There's probably just one simple little detail we both have overlooked.
0
 

Author Comment

by:mattgoolsby
ID: 12199579
Here is the code that I think is pertinent.  I will post more if needed.

ASPX HTML Code:

<form id="Form1" method="post" runat="server">
<p><b><asp:label id="lblSchedule" runat="server" cssclass="ScheduleTable">Create New Schedule</asp:label></b></p>
<TABLE id="Table1" style="LEFT: 10px; POSITION: absolute; TOP: 59px" height="464" cellSpacing="1" cellPadding="5" width="916" border="0">
      <TR>
            <TD align="right" width="212"><asp:label id="Label1" runat="server" CssClass="ScheduleTable">Budget:</asp:label></TD>
            <TD><asp:dropdownlist id="cboBudget" runat="server" CssClass="ScheduleTable" AutoPostBack="True" Width="400px"></asp:dropdownlist><asp:label id="lblBudget" runat="server" CssClass="ScheduleTable">Label</asp:label>
            <asp:RequiredFieldValidator id="valBudget" runat="server" ErrorMessage="Please Select a Budget" ControlToValidate="cboBudget" EnableClientScript="False" CssClass="ScheduleTable"></asp:RequiredFieldValidator></TD>
      </TR>
      <TR>
            <TD align="right" width="212"><asp:label id="Label2" runat="server" CssClass="ScheduleTable">Schedule Type:</asp:label></TD>
            <TD><asp:dropdownlist id="cboSchedule" runat="server" CssClass="ScheduleTable" Width="192px"></asp:dropdownlist><asp:label id="lblScheduleType" runat="server" CssClass="ScheduleTable">Label</asp:label>
            <asp:CustomValidator id="valSchedule" runat="server" CssClass="ScheduleTable" ErrorMessage="Please Enter a Schedule Type"
ControlToValidate="cboSchedule" EnableClientScript="False"></asp:CustomValidator></TD>
      </TR>

CODE BEHIND:

Private Sub cmdSaveBase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveBase.Click

            If Page.IsValid Then
                'Other code in here
            End If
End Sub

Sub valSchedule_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles valSchedule.ServerValidate

        If Request.QueryString("Edit") <> "yes" Then

            If cboSchedule.SelectedItem.Value = "" Then
                args.IsValid() = False
            End If
        End If

End Sub

0
 
LVL 2

Accepted Solution

by:
Arvaris earned 50 total points
ID: 12199615
Remove the ControlToValidate statement since you're manually identifying the control your validating in the function anyways.

I think I've had issues with that before
0
 

Author Comment

by:mattgoolsby
ID: 12199651
That worked perfectly.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question