We help IT Professionals succeed at work.
Get Started

Problem with CustomValidator

jcpatterson asked
Last Modified: 2008-01-09
I have a company list that displays query results in a dropdownlist.  I want to validate that the user has selected a company.  The first item that displays on the list is Select Company.  The page renders correctly but I am not getting an error message when Select Company is not changed.

This is the code generating the dropdown list in the Code Behind page.  You will notice the Select Company as a new list item:

Protected Sub ddlCompany_DataBound(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddl As DropDownList = CType(sender, DropDownList)
        ddl = CType(frmProject.FindControl("CompanyDropDownList"), DropDownList)
        Dim li As New ListItem("Select Company", "")
        ddl.Items.Insert(0, li)
    End Sub

This is the listbox and validation code being used:

      <script language="vbscript" runat="server">
          Sub ValidateCompany(ByVal s As Object, ByVal e As ServerValidateEventArgs)
              If (e.Value <> "Select Company") Then
                  e.IsValid = True
              End If
          End Sub

<asp:CustomValidator ID="EvalCompanyDropDownList" ControlToValidate="CompanyDropDownList" OnServerValidate="ValidateCompany" ErrorMessage="Select Company" runat="server" />

<asp:DropDownList id="CompanyDropDownList" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" OnDataBound="ddlCompany_DataBound" DataSourceid="SqlDataSourceCo" DataTextField="Name" DataValueField="HQCo"></asp:DropDownList>

Your help would be appreciated.

Thank you.

Watch Question
Top Expert 2007
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE