Solved

Custom Validator Not Working

Posted on 2004-09-30
8
619 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
  • 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
 

Author Comment

by:mattgoolsby
ID: 12199494
EnableClientScript is false on both controls
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now