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