Solved

CustomValidator problem

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now