Solved

CustomValidator problem

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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