Avatar of jcpatterson
jcpatterson asked on

Problem with CustomValidator

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
      </script>

<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.

John
ASP.NET

Avatar of undefined
Last Comment
McExp

8/22/2022 - Mon
McExp

You don't need a Custom Validator in this instance a simpler solution would be to use a RequiredFieldValidator and set the InitalValue to "Select Company"
ASKER
jcpatterson

Hi there,

The idea is to make sure a company is selected.  The required field validator set to initialvalue to Select Company does not tell me if a project was selected.  Your example would allow the user to submit with Select Company as the project selected.

John
ASKER CERTIFIED SOLUTION
McExp

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
McExp

Have you resolved your problems?

If so can you close it off?

We might even win the laptops!!!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck