Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CustomValidator problem

Posted on 2006-11-07
1
Medium Priority
?
795 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
[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
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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…

715 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