Search code examples
asp.netvalidationrequiredfieldvalidator

RequiredFieldValidator not Validating


I have a control on an ASP.NET page with a required field validator. Only problem is that when I go to change pages via my page buttons it doesn't cause validation. Here is the code, can anyone see what's wrong?

<asp:Label ID="lblD_Year" runat="server" BorderStyle="None" 
                       Text="Fiscal Year" CssClass="h2"></asp:Label>&nbsp;&nbsp;&nbsp;
                <asp:DropDownList ID="ddlD_Year" runat="server" Width="100px" TabIndex="8" 
                    AutoPostBack="True" CssClass="box" CausesValidation="True">
                    <asp:ListItem Value="0" Selected="True">Select Year</asp:ListItem>
                </asp:DropDownList>                               

                <asp:RequiredFieldValidator ID="rfvD_Year" runat="server" 
                    ControlToValidate="ddlD_Year" Display="Dynamic" ErrorMessage="*" 
                    Font-Bold="True" ForeColor="Red" InitialValue="Select Year" 
                    SetFocusOnError="True"></asp:RequiredFieldValidator>

Solution

  • set initial value as InitialValue="-1" for validator or any value which you consider to be empty, if selected