Solved

CustomValidator problem

Posted on 2006-11-07
1
792 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 500 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

622 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