Solved

CustomValidator is not fired!

Posted on 2006-11-06
10
478 Views
Last Modified: 2007-12-19
Hi,  CustomValidator control is not fired when I submit the form, here is the code:


<asp:CustomValidator ControlToValidate="Departments" OnServerValidate="HasDepartment" Text="Employee should be assigned to a department" runat="server" ID="Departmentsvld" />


   Public Sub HasDepartment(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        Dim blnHasDepartment As Boolean
        blnHasDepartment = False
        Dim LiThisOne As ListItem
        For Each LiThisOne In Departments.Items
            If LiThisOne.Selected Then
                blnHasDepartment = True
            End If
        Next
        If blnHasDepartment = True Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If

    End Sub

Do someone know what I am doing wrong?
0
Comment
Question by:Abdu_Allah
[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
  • 5
  • 3
  • 2
10 Comments
 
LVL 13

Assisted Solution

by:jeebukarthikeyan
jeebukarthikeyan earned 100 total points
ID: 17881214
hi,

i will site a sample example

<asp:CustomValidator runat="server" id="custPrimeCheck"
ControlToValidate="txtPrimeNumber"
OnServerValidate="PrimeNumberCheck"
ErrorMessage="Invalid Prime Number" />

<script language="JavaScript">
<!--
  function CheckPrime(sender, args)
  {
    var iPrime = parseInt(args.Value);
    var iSqrt = parseInt(Math.sqrt(iPrime));

    for (var iLoop=2; iLoop<=iSqrt; iLoop++)
      if (iPrime % iLoop == 0)
      {
         args.IsValid = false;
         return;
      }

    args.IsValid = true;
  }
// -->
</script>

may be ur problem lies in the script

b u d d h a


0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881425
I cannot see anything that may cause a problem!
0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 17881520
hi,

can u just put ur javascript here

b u d d h a
0
Industry Leaders: 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!

 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881570
What javascript?
0
 
LVL 13

Expert Comment

by:jeebukarthikeyan
ID: 17881664
hi,
the validation script

b u d d h a
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881695
It is in the question body.
Here it is again:

 Public Sub HasDepartment(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        Dim blnHasDepartment As Boolean
        blnHasDepartment = False
        Dim LiThisOne As ListItem
        For Each LiThisOne In Departments.Items
            If LiThisOne.Selected Then
                blnHasDepartment = True
            End If
        Next
        If blnHasDepartment = True Then
            args.IsValid = True
        Else
            args.IsValid = False
        End If

    End Sub
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17881702
I used server side validation script and not client-side.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17883070
If you are using IE to test the validation, a custom validator with no "OnClientValidate" it will not fire until a postback happens. If you want it to fire when the controltovalidate loses focus then you need to have the javascript function on the page that will do client side validation.
0
 
LVL 3

Author Comment

by:Abdu_Allah
ID: 17884716
So how can I check if the listbox has a selected item(s) or not using javascript?
0
 
LVL 28

Accepted Solution

by:
strickdd earned 150 total points
ID: 17888621
for(int i=0; i<document.GetElementById("ListBoxIDHere").length; i++)
{
     if(document.GetElementById("ListBoxIDHere")[i].selected)
     {
        //this is selected
     }
}


The syntax is close. I would worry most about my capitalization.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Json and ajax 1 41
ASP.NET data base connection 35 81
dropdownlist in asp.net vb. 3 18
bulk load multiple users for aspnetusers table 2 7
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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