Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CustomValidator problem

Posted on 2006-11-07
1
Medium Priority
?
801 Views
Last Modified: 2008-02-01
I am trying to validate listbox by CustomValidator control using client script but the clien script function is not fired!
Here is my code, what I am doin wrong?

<asp:listbox id="Departments" runat="server" Width="160px" SelectionMode="Multiple"></asp:listbox>
<asp:CustomValidator id="Departmentsvld" runat="server" ControlToValidate="Departments"  ClientValidationFunction="HasDepartment" Text="*" />

<script>                  
function HasDepartment(source, arguments)
      {
            alert("Fired");
            for(var i=0; i < document.getElementById('Departments').length; i++)
            {
                  if(document.getElementById("Departments")[i].selected)
                  {
                        arguments.IsValid = true;
                        return;
                        //this is selected
                  }
            }
            arguments.IsValid = false;
      }
</script>
0
Comment
Question by:Abdu_Allah
1 Comment
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 2000 total points
ID: 17890379
There is probably some Javascript syntax error somewhere else in your page that keeps HasDepartment from being "seen".

Just using a mockup codefile and your code in a fresh page (both below), I get the "Fired" alert everytime I click in the selection box.

So whatever your problem is, it is NOT in the code which you posted!


code behind
------------------------------------
Partial Class eelb
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Departments.Items.Add(New ListItem("Bakery", "20"))
            Departments.Items.Add(New ListItem("Grocery", "30"))
            Departments.Items.Add(New ListItem("Pharmacy", "40"))
            Departments.Items.Add(New ListItem("Produce", "50"))
            Departments.Items.Add(New ListItem("Meat", "60"))
            Departments.Items.Add(New ListItem("Fish", "70"))

        End If
    End Sub
End Class
---------------------------

page code
---------------------------
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="eelb.aspx.vb" Inherits="eelb" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:listbox id="Departments" runat="server" Width="160px" SelectionMode="Multiple"></asp:listbox>
<asp:CustomValidator id="Departmentsvld" runat="server" ControlToValidate="Departments"  ClientValidationFunction="HasDepartment" Text="*" />

<script>              
function HasDepartment(source, arguments)
     {
          alert("Fired");
          for(var i=0; i < document.getElementById('Departments').length; i++)
          {
               if(document.getElementById("Departments")[i].selected)
               {
                    arguments.IsValid = true;
                    return;
                    //this is selected
               }
          }
          arguments.IsValid = false;
     }
</script>
    </div>
    </form>
</body>
</html>
---------------------------

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

972 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